Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

driving LEDs with a Relay

Status
Not open for further replies.

t.man

New Member
i need to drive a couple of LEDs (8 or 10 of em) using a relay form a uC pin. can someone suggest a circuit configuration/drawing. i have DIP05-1A72-12L from MEder Electronic.

kind regards
 

Attachments

  • 36_en_1_DIP-Series_e.pdf
    875.6 KB · Views: 219
i need to drive a couple of LEDs (8 or 10 of em) using a relay form a uC pin. can someone suggest a circuit configuration/drawing. i have DIP05-1A72-12L from MEder Electronic.

kind regards

hi,
What colour and forward voltage drop are the LED's.?

I assume they are the 15mA to 20mA type.
 
they are the typical green and red ones, i normally use 220ohm if i have to connect it directly to my uC.

Hi,
Reason for asking, is the difference in Vfwd. Look at the image.

You can connect LED's in series and use a single resistor.

Say you have a +12Vsupply, you could connect 4 or 5 Red LED's in series and choose a resistor to allow 15mA thru the LED's.

If You only have a +5V supply, connect 2 Red LED's in series with a series resistor.

The relay coil [from the other post] has a 500R coil, so it can be connected to the PIC output pin.

Does this cover your question.?:)
 

Attachments

  • LEDfwd1.gif
    LEDfwd1.gif
    6.1 KB · Views: 181
Hi,
Reason for asking, is the difference in Vfwd. Look at the image.

You can connect LED's in series and use a single resistor.

Say you have a +12Vsupply, you could connect 4 or 5 Red LED's in series and choose a resistor to allow 15mA thru the LED's.

If You only have a +5V supply, connect 2 Red LED's in series with a series resistor.

The relay coil [from the other post] has a 500R coil, so it can be connected to the PIC output pin.

Does this cover your question.?:)

am i correct?
 

Attachments

  • rl.PNG
    rl.PNG
    37.6 KB · Views: 203
am i correct?

hi,
Looks OK, if its 5V and the Red LED's drop say 2V each, you can only have 2 LED's in series.
[ 2 * 2=4v] leaving 1 volt to drop with the resistor.

If its 12V and the Red LED's drop 2V each then you could have 5 LED's in series [thats 5 *2 = 10v]
and use a series resistir to drop the remaining 2Volts.
If the LED's are 15mA, then the resistor would be Rs= 2/0.015 = 133R say 120R.
 

Attachments

  • rl.PNG
    rl.PNG
    18 KB · Views: 157
Last edited:
i'm trying to test it but with no luck. i'm using 5v with 1 LED and 220ohm. what might be the problem now?
 
i'm trying to test it but with no luck. i'm using 5v with 1 LED and 220ohm. what might be the problem now?

hi,
A number of reasons.
First test the LED by using the 220R in series and connect across the 5V.
The LED should light, remember the cathode of the LED goes to 0V.

If that works OK, then connect the the LED and resistor to the relay as per you drawing. using pins 14 and 8

If the LED dosn't light you may have a defective LED or the 220R isnt a 220R but some higher value.!

Connect +5V to pin 2 of the relay coil and the 0V to pin 6 of the relay.

When you do that the relay should operate, you may hear the relay click over and the LED should light.

EDIT: you do these tests with the relay and LED's, NOT connected to the PIC
 

Attachments

  • rl.PNG
    rl.PNG
    20 KB · Views: 153
Last edited:
hi,
A number of reasons.
First test the LED by using the 220R in series and connect across the 5V.
The LED should light, remember the cathode of the LED goes to 0V.

If that works OK, then connect the the LED and resistor to the relay as per you drawing. using pins 14 and 8

If the LED dosn't light you may have a defective LED or the 220R isnt a 220R but some higher value.!

Connect +5V to pin 2 of the relay coil and the 0V to pin 6 of the relay.

When you do that the relay should operate, you may hear the relay click over and the LED should light.

EDIT: you do these tests with the relay and LED's, NOT connected to the PIC

following the above instructions, everything works fine...then where do i connect uC pin. pin 6?
again, will the relay switch when i send logic ZERO from the uC?
 
following the above instructions, everything works fine...then where do i connect uC pin. pin 6?
again, will the relay switch when i send logic ZERO from the uC?

hi,
Thats good.

Connect pin 6 of the relay to an OUTPUT pin of the PIC.
In the program you must configure the PIC pin as an output.

Which PIC are you using, can you post a program.?

EDIT:
If you want to add a transistor driver for the relay, see the image.

With the relay connected to the PIC pin without a transistor, when the PIC powers up, the relay will switch ON, until you make the pin high.

Using a transistor OR by connecting pin 2 of the relay to the PIC output pin and pin 6 of the relay to 0V, will mean the relay will not operate until you set the PIC pin high....OK.

NOTE:
You cannot always connect a relay coil directly to a PIC pin.
As your relay is 5Vdc and is 500R, that is 10mA its OK.
 

Attachments

  • rl.PNG
    rl.PNG
    22.8 KB · Views: 156
  • rl2.PNG
    rl2.PNG
    23.3 KB · Views: 157
Last edited:
Which PIC are you using, can you post a program.?

EDIT:
If you want to add a transistor driver for the relay, see the image.

With the relay connected to the PIC pin without a transistor, when the PIC powers up, the relay will switch ON, until you make the pin high.

Using a transistor OR by connecting pin 2 of the relay to the PIC output pin and pin 6 of the relay to 0V, will mean the relay will not operate until you set the PIC pin high....OK.

NOTE:
You cannot always connect a relay coil directly to a PIC pin.
As your relay is 5Vdc and is 500R, that is 10mA its OK.

i'm using AVR, mega16,.
actually i need this configuration as part of my display (indicator) system...i have LCD display and i will have green and red LEDs as indicators( as u can see i ougt to have a cluster of them so that they can be visible at a distant).

I read analog values through uC ADC and write them to my lcd and LEDs will light depending on the values i get.
 
i'm using AVR, mega16,.
actually i need this configuration as part of my display (indicator) system...i have LCD display and i will have green and red LEDs as indicators( as u can see i ougt to have a cluster of them so that they can be visible at a distant).

I read analog values through uC ADC and write them to my lcd and LEDs will light depending on the values i get.

hi,
Whats the output pin current specification of the AVR.?
I dont use AVR's, make sure it can sink/source at least 10mA.
 
hi,
Whats the output pin current specification of the AVR.?
I dont use AVR's, make sure it can sink/source at least 10mA.

DC current per I/O pin is 40.0mA

main.c
Code:
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/interrupt.h>


#include "lcd.h"
#define V_ref 5.00
volatile double V_adc;
unsigned char CLRW[] = "                ";
unsigned char text[] = "dose:";
unsigned char ready[] = "Ready    ";
unsigned char alarm[] = "Alarm on!";
unsigned char high[] = "dose high";
unsigned char ADC_cnt = 0; // variable to count number of ADC calcualtions
int ADCresult_average = 0;// integer to keep the ADC results for later averaging





//adjust LCD_putc() function for stream
static int LCD_putstream(char c, FILE *stream);

//set output stream to LCD
static FILE mystdout = FDEV_SETUP_STREAM(LCD_putstream, NULL,
                                             _FDEV_SETUP_WRITE);

void delay1s(void);
void init_ADC(unsigned char channel);
void AD_conversion(void);
void dose_calculation(int);


int main()
{
	stdout = &mystdout;//init stream
	DDRD = 0b01110000;
	
	 init_LCD();//init LCD

	 init_ADC(0x00);

	 //initialize free running tmr1 with interrupt every seconds
	TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
	TIMSK |= (1 << OCIE1A); // Enable CTC interrupt

	sei(); //  Enable global interrupts

	OCR1A   = 62500; /* Set CTC compare value to 2Hz at 1MHz AVR clock,
						 with a prescaler of 8 */
					
	TCCR1B |= (1 << CS11);// Start timer at Fcpu/8  

	//send some characters to my display
	printf("AVR Project");
	delay1s();
	LCD_puts(CLRW,0,0);
	LCD_puts(text,0,1);

	
	for( ; ; )
	{
		AD_conversion();
		}
	



}


static int LCD_putstream(char c, FILE *stream)
{
	LCD_putc(c);
	return 0;
}

void delay1s(void)
{
	uint8_t i;
	for(i=0;i<100;i++)
	{
	  _delay_ms(10);
	}
}
void init_ADC(unsigned char channel)
{
	 /*select ADC channel and 
	   reference voltage(AVCC with external capacitor at AREF pin)*/

   	 ADMUX |=(1<<REFS0)|(channel);

	 _delay_ms(50); //delay in order for channel selection to settle

	  
	// set ADC prescaler to , 1MHz / 8 = 125kHz
	ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);
	
	// run a dummy conversion the first time the ADC is used after a power up 
	AD_conversion();

}
void AD_conversion(void)
{
    int ADC_temp;
    int ADCresult = 0;
    unsigned char i;
	
	
	
   for(i=0;i<8;i++)            // do the ADC conversion 8 times for better accuracy
    {
        ADCSRA |= (1<<ADSC);        // do single conversion
        while(!(ADCSRA & (1<<ADIF)));// wait for ADIF flag to be set, conversion complete

        ADC_temp = ADCL;            // read out ADCL register
        ADC_temp += (ADCH << 8);    // read out ADCH register

        ADCresult += ADC_temp;      // accumulate result (8 samples) for later averaging
    }

    ADCresult = ADCresult >> 3;     // average the 8 samples

    ADC_cnt++;           // increment the counter that keeps track of how many calulations that have been donet

    ADCresult_average += ADCresult; // add the ADCresult to the previous measurements

    if(ADC_cnt >= 32)    // if 32 calculations
    {
        ADC_cnt = 0;     // clear the counter

        ADCresult_average = ADCresult_average >> 5;   // find the average ADC result for the last 32 times

        dose_calculation(ADCresult_average);// do calculations

        ADCresult_average = 0;      // clear the ADCresult_average
    }
}

void dose_calculation(int ADC_value)
{
	
		V_adc = ((ADC_value * V_ref)/1024);//find input analog voltage

		if(V_adc < 2.00)
		{	
			PORTD = 0b10111111;
			}
			else if(V_adc > 2.00 && V_adc <= 3.50)
			{
				PORTD = 0b11011111;
				}
				else
				{	PORTD |=(1<<4); 
					PORTD ^= 0b00100000;
				}
}

//refresh display
ISR(TIMER1_COMPA_vect) 
{ 
	if(V_adc < 2.00)
	{	
	  LCD_puts(ready,0,0);
	  }
	 else if(V_adc > 2.00 && V_adc <= 3.50)
			{
			  LCD_puts(high,0,0);
				}
				else
				{
					LCD_puts(alarm,0,0);
					}

		
   LCDGotoXY(0x07,0x01);
   printf("%.2lfV",V_adc);
}

lcd.c
Code:
#include <avr/io.h>
#include <util/delay.h>
#include "lcd.h"


void LCD_enable()
{
    LCD_CTRL_PORT |= (1<<LCD_en); 
    _delay_ms(1);
    LCD_CTRL_PORT &= ~(1<<LCD_en); 
}

void LCD_nibble(unsigned char nibble){
    LCD_CTRL_PORT &= ~(1<<LCD_rs); 
    LCD_DATA_PORT = (nibble & 0xF0)|(LCD_DATA_PORT & 0x0F); 
    LCD_enable(); 
}

void LCD_command(unsigned char command)
{
    LCD_CTRL_PORT &= ~(1<<LCD_rs); /*rs LOW for instruction write */
    LCD_DATA_PORT = (command & 0xF0)|(LCD_DATA_PORT & 0x0F);
    LCD_enable();
    LCD_DATA_PORT = (command << 4 & 0xF0)|(LCD_DATA_PORT & 0x0F);
    LCD_enable();
    _delay_ms(1);
}

void LCD_putc(unsigned char ascii)
{
    LCD_CTRL_PORT |= (1<<LCD_rs); /* rs HIGH for data write */
    LCD_DATA_PORT = (ascii & 0xF0)|(LCD_DATA_PORT & 0x0F);
    LCD_enable();
    LCD_DATA_PORT = (ascii << 4 & 0xF0)|(LCD_DATA_PORT & 0x0F);
    LCD_enable();
    _delay_ms(1);
}

void LCD_puts(unsigned char *lcd_string, unsigned char x, unsigned char y)
{
   LCDGotoXY(x,y);
   while (*lcd_string) 
	{
		LCD_putc(*lcd_string++);
	}
}

void LCDGotoXY(uint8_t x, uint8_t y)	//Cursor to X Y position
{
	register uint8_t DDRAMAddr = 0x00;
	// remap lines into proper order
	switch(y)
	{
		case 0: DDRAMAddr = LCD_LINE0_DDRAMADDR+x; break;
		case 1: DDRAMAddr = LCD_LINE1_DDRAMADDR+x; break;
		default: DDRAMAddr = LCD_LINE0_DDRAMADDR+x;
	}
	// set data address
	LCD_command(1<<LCD_DDRAM | DDRAMAddr);
	
}

void init_LCD() 
{ 

   //initialize LCD control lines 
   LCD_CTRL_PORT &= ~(1<<LCD_rs);   // RS low 
   LCD_CTRL_PORT &= ~(1<<LCD_rw);   // R/W low 
   LCD_CTRL_PORT &= ~(1<<LCD_en);   // E low 

   // initialize LCD control lines to output 
   LCD_CTRL_DDR |= (1<<LCD_rs); 
   LCD_CTRL_DDR |= (1<<LCD_rw); 
   LCD_CTRL_DDR |= (1<<LCD_en); 

   // initialize LCD data port to input 
    LCD_DATA_DDR |= 0xF0;// Data on high four bits of port 

    _delay_ms(15);		//start of required sequnce
    LCD_nibble(0x30);
    _delay_ms(5); 
    LCD_nibble(0x30);
    _delay_ms(1); 
    LCD_nibble(0x30);	
    _delay_ms(1); 
    LCD_nibble(0x20);		//switch to 4 bit mode


   LCD_function_set();
   _delay_us(40); 
   LCD_display_off(); 
   _delay_us(40); 
   LCD_display_clear(); 
   _delay_ms(2); 
   LCD_entry_mode_set(); 
   LCD_display_on(); 
}

lcd.h
Code:
#ifndef LCD_H 
#define LCD_H

//define lcd data port
#define LCD_DATA_PORT PORTB
#define LCD_DATA_DDR DDRB

	

//define lcd control port
#define LCD_CTRL_PORT PORTB
#define LCD_CTRL_DDR DDRB
#define LCD_rs 0
#define LCD_rw 1
#define LCD_en 2
#define LCD_DDRAM 7

#define LCD_LINE0_DDRAMADDR		0x00
#define LCD_LINE1_DDRAMADDR		0x40



// LCD instructions
#define LCD_start() LCD_command(0x20)	/* Set to origin LCD */
#define LCD_function_set() LCD_command(0x28) /* LCD function set */
#define LCD_display_off() LCD_command(0x08) /* LCD display off*/
#define LCD_display_on() LCD_command(0x0C) /* LCD display on*/
#define LCD_display_clear() LCD_command(0x01)/* Clear display LCD */
#define LCD_entry_mode_set() LCD_command(0x06) /* LCD mode set */



//functions declarations
void LCD_enable();
void LCD_command(unsigned char command);
void LCD_putc(unsigned char ascii);
void LCD_puts(unsigned char *lcd_string, unsigned char x, unsigned char y);
void init_LCD();
void LCDGotoXY(uint8_t x, uint8_t y);
void LCD_nibble(unsigned char nibble);

#endif

like i said the idea is to read ADC values(from radiation detector) and display the readings on LCD and lit LEDs.
dnt worry about calculations in dose_calculation( there will be some calculations to do conversions from voltages to radiation quantities or something like that).

any suggestions for change?
i may add an alarm/buzzer as well as some serial communications.
 
t.man;656829]DC current per I/O pin is 40.0mA
That current is OK.

hi,
I write in assembler, not C.:(

The best guy to help you thru this program is Pommie aka Mike.:)
 
Have you tried changing your for loop to something like,

Code:
    for( ; ; )
    {
        PORTD = 0b10111111;	//Light LED1
        DelayMs(250);		//some delay routine
        PORTD = 0b11011111;	//Light LED2
        DelayMs(250);		//delay some more
        PORTD = 0b11101111;	//Light LED3
        DelayMs(250);		//and more
    }

Just to check that the relays/LEDs actually work. I assume there is a delay mS function or equivalent available.

Mike.
 
Have you tried changing your for loop to something like,

Code:
    for( ; ; )
    {
        PORTD = 0b10111111;	//Light LED1
        DelayMs(250);		//some delay routine
        PORTD = 0b11011111;	//Light LED2
        DelayMs(250);		//delay some more
        PORTD = 0b11101111;	//Light LED3
        DelayMs(250);		//and more
    }

Just to check that the relays/LEDs actually work. I assume there is a delay mS function or equivalent available.

Mike.

yes, relays/LEDs are working. by connecting Pin 2 to +5V and pin 6 to the uC pin, one need to send logic Zero from uc pin to switch the relay ON and vise versa.
 
yes, relays/LEDs are working. by connecting Pin 2 to +5V and pin 6 to the uC pin, one need to send logic Zero from uc pin to switch the relay ON and vise versa.

Are you saying that the LEDs light one after the other with the above code?

Mike.
 
Last edited:
I have no idea what pins 2 and 6 etc are. Are you saying that the LEDs light one after the other with the above code?

Mike.

Morning Mike,
Repost of the way the DIL 5V relay is connected, the pin # are the relay.

Personally I would have just used a transistor to drive the LED's.
 

Attachments

  • rl.PNG
    rl.PNG
    18 KB · Views: 160
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top