Society of Robots - Robot Forum

Software => Software => Topic started by: corrado33 on April 17, 2010, 02:51:53 PM

Title: Can I really not turn on a simple LED?
Post by: corrado33 on April 17, 2010, 02:51:53 PM
Ok, well apparently I have conquered PWM and dimming LEDs, but when it comes to turning on a simple LED with no gimmicks I can't do it...

What's up?  So I set a pin to output (set the bit to 1), and nothing happens.  Shouldn't setting a pin high send +5V through it?  I'm confused  ???

I looked through the SoR-Utils that came with the photovore program a long time ago, and I tried to change the LED_on function to port D7, and I looked up an LED straight to it (I know I should have used a resistor).  I turned the LED on by using the function, and nothing happened.  At first I thought that maybe the program set up the ports wrong, then I noticed that in the photovore program all of the D ports were to set to output (PORTD|=0xFF).  Then to turn the led ON the program turned the port OFF.  Why?  I already had D7 set to output so that wasn't a problem.  What's going on?  Here's the code I would use to try to turn on an LED on Port D7 on an atmega8.

Code: [Select]
DDRD|=(1<<PORTD7);That sets the port to output.  I thought this itself would turn the LED on (or at least supply voltage to that port, which it doesn't, I checked with a multimeter.)

Code: [Select]
PORTD &= ~(1<<7);
I copied this from the SoR-utils header file.  That is the code it would use to turn the LED ON.  On port seven that is. 

So what's going on... I'm really frustrated...

Oh, I have other things connected to the MCU (I didn't feel like dissembling my LED dimmer circuit), but that shouldn't affect anything right?
Title: Re: Can I really not turn on a simple LED?
Post by: Razor Concepts on April 17, 2010, 02:56:14 PM
DDRB solely changes whether a pin is output or input, it does not change the state.

PORTB solely changes the state of the pin (either HIGH or LOW)

So, to turn the LED on, you need to set the pin to output, then change the state of the pin to whatever turns it on (depends on how you wired it).

Title: Re: Can I really not turn on a simple LED?
Post by: corrado33 on April 17, 2010, 03:04:11 PM
DDRB solely changes whether a pin is output or input, it does not change the state.

PORTB solely changes the state of the pin (either HIGH or LOW)

So, to turn the LED on, you need to set the pin to output, then change the state of the pin to whatever turns it on (depends on how you wired it).



OOOOOOOOOOOOOOOOOOOOOOOOO...  I KNEW there had to be something like that.  Hmmm, now what exactly does the second snippet of code do that I have written above?  In the SoR-utilities it says it turns the port OFF.  Why does that turn the led ON?

EDIT:  I see, there are three registers for portB or C or D.  There is DDRD, PORTD, and PIND.  I see what DDRD and PORT do, but what does PIN do?  I remember reading this somewhere, but I can't remember exactly.  I kind of glazed over it...

EDIT:  Oh and the reason is wasn't working when I used the photovore program... I had the wiring shifted one row up on the breadboard :oops:
Title: Re: Can I really not turn on a simple LED?
Post by: Razor Concepts on April 17, 2010, 03:47:47 PM
Turning the port "off" doesn't really turn it off. It just sets it low (0v). So, if the cathode of the LED is on that pin, and the pin is set low, current can flow, and the LED lights - so that is how turning a pin "off" turns the LED on. Off is generally incorrect terminology, low or high is a better way to name it.

As for the PIN registers:
Quote
PIND is the register to read the state of pins, PORTD is the register to output things on pins
Title: Re: Can I really not turn on a simple LED?
Post by: corrado33 on April 17, 2010, 06:37:35 PM
That makes sense, but it doesn't work like that in my case.  Here's the programming I had to use to get the LED to turn ON.

Code: [Select]
DDRD|=(1<<PORTD7);
PORTD|=(1<<7);

You can see that I set pin D7 for output, then I set Port D7 high, and my LED lights!  The cathode on an LED is the + end right?  Therefore it's the longer end?  I have the longer end connected to pin D7. 
Title: Re: Can I really not turn on a simple LED?
Post by: SmAsH on April 17, 2010, 06:43:35 PM
The cathode on an LED is the + end right?  Therefore it's the longer end?  I have the longer end connected to pin D7. 
Oh dear... The annode is the + end, it is longer.
Title: Re: Can I really not turn on a simple LED?
Post by: corrado33 on April 17, 2010, 07:34:31 PM
But but but... cations (in chemistry) are positive... oooo a cathode is named because cations migrate toward it... wow I feel dumb now...
Title: Re: Can I really not turn on a simple LED?
Post by: SmAsH on April 17, 2010, 08:19:18 PM
But but but... cations (in chemistry) are positive... oooo a cathode is named because cations migrate toward it... wow I feel dumb now...
Don't, the concept still gets me all the time. How electrons flow from positive to negative confuses the heck out of me :P Just leave that aside and think anode (long leg) is positive.
Title: Re: Can I really not turn on a simple LED?
Post by: tim_wang on April 17, 2010, 08:54:24 PM
DDRB solely changes whether a pin is output or input, it does not change the state.

PORTB solely changes the state of the pin (either HIGH or LOW)

So, to turn the LED on, you need to set the pin to output, then change the state of the pin to whatever turns it on (depends on how you wired it).



OOOOOOOOOOOOOOOOOOOOOOOOO...  I KNEW there had to be something like that.  Hmmm, now what exactly does the second snippet of code do that I have written above?  In the SoR-utilities it says it turns the port OFF.  Why does that turn the led ON?

EDIT:  I see, there are three registers for portB or C or D.  There is DDRD, PORTD, and PIND.  I see what DDRD and PORT do, but what does PIN do?  I remember reading this somewhere, but I can't remember exactly.  I kind of glazed over it...

EDIT:  Oh and the reason is wasn't working when I used the photovore program... I had the wiring shifted one row up on the breadboard :oops:

PIND is the register used to read the value of port D.

PORTD &= ~(1<<7); sets port D pin 7 to 0. This turns on the LED because it grounds the LED's negative pin (cathode).
Title: Re: Can I really not turn on a simple LED?
Post by: SmAsH on April 17, 2010, 09:17:10 PM
PIND is the register used to read the value of port D.

PORTD &= ~(1<<7); sets port D pin 7 to 0. This turns on the LED because it grounds the LED's negative pin (cathode).
But the annode was connected to D7?
Quote
I have the longer end connected to pin D7.  

Corrado33, where did you have the other end of the led connected to?
Title: Re: Can I really not turn on a simple LED?
Post by: tim_wang on April 18, 2010, 12:23:19 AM
PIND is the register used to read the value of port D.

PORTD &= ~(1<<7); sets port D pin 7 to 0. This turns on the LED because it grounds the LED's negative pin (cathode).
But the annode was connected to D7?
Quote
I have the longer end connected to pin D7.  

Corrado33, where did you have the other end of the led connected to?

Wire up the LED this way:

(http://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/LED_circuit.svg/100px-LED_circuit.svg.png)

With the anode (longer wire) going to the positive terminal of the battery, and the cathode (short wire) going to port d pin7. The resister can go between either the anode and the positive terminal of the battery, or between the cathode and port d pin 7. The resister just limits the amount of current going through the LED so it does not get fried.

You should NOT connect the anode (longer wire) to port d pin 7 because the microcontroller pins can only source (provide) a tiny amount of current. While this tiny amount of current was able to power 1 LED,  it would fail if connected to a bank of LEDs in parallel, or other more power hungry devices.
Title: Re: Can I really not turn on a simple LED?
Post by: Razor Concepts on April 18, 2010, 07:14:27 AM


You should NOT connect the anode (longer wire) to port d pin 7 because the microcontroller pins can only source (provide) a tiny amount of current. While this tiny amount of current was able to power 1 LED,  it would fail if connected to a bank of LEDs in parallel, or other more power hungry devices.

Well, technically these AVRs can both sink and source the same amount of current. It is just recommended to sink instead of source so that the AVR chip doesn't have to provide current.
Title: Re: Can I really not turn on a simple LED?
Post by: tim_wang on April 18, 2010, 10:24:32 AM


You should NOT connect the anode (longer wire) to port d pin 7 because the microcontroller pins can only source (provide) a tiny amount of current. While this tiny amount of current was able to power 1 LED,  it would fail if connected to a bank of LEDs in parallel, or other more power hungry devices.

Well, technically these AVRs can both sink and source the same amount of current. It is just recommended to sink instead of source so that the AVR chip doesn't have to provide current.

Well that is true, but I never said the microcontroller I/O pins can sink more current than it can source.  ;)
Title: Re: Can I really not turn on a simple LED?
Post by: Webbot on April 18, 2010, 06:26:24 PM
Well that is true, but I never said the microcontroller I/O pins can sink more current than it can source.  ;)
Sadly, that is true. You didn't tell us that fact.  :'(

But luckily Razor Concepts quite rightly told us that you could. :)

Hasn't this subject been 'talked to death'? Its come up at least 8 times in the last 12 months I reckon.

Edit: for example http://www.societyofrobots.com/robotforum/index.php?topic=10690.0 (http://www.societyofrobots.com/robotforum/index.php?topic=10690.0)
Title: Re: Can I really not turn on a simple LED?
Post by: corrado33 on April 18, 2010, 06:29:37 PM
So, buy setting the port LOW, i'm letting current flow from the battery, therefore letting the led light?  What happens when it's HIGH?  Isn't that basically putting a battery (or at least a voltage source) on both sides of the LED?  I can't see that being good?
Title: Re: Can I really not turn on a simple LED?
Post by: Webbot on April 18, 2010, 06:35:11 PM
Perhaps other users may suggest a  'beginners guide to electronics'
Title: Re: Can I really not turn on a simple LED?
Post by: SmAsH on April 18, 2010, 06:51:15 PM
So, buy setting the port LOW, i'm letting current flow from the battery, therefore letting the led light?  What happens when it's HIGH?  Isn't that basically putting a battery (or at least a voltage source) on both sides of the LED?  I can't see that being good?
Well, if its just one led, i suggest putting the anode to the digital port and sending the current through the led to ground, if both are low, nothing is bad. :)
Title: Re: Can I really not turn on a simple LED?
Post by: corrado33 on April 18, 2010, 08:47:41 PM
Perhaps other users may suggest a  'beginners guide to electronics'


Yeah I do need to read up on my electronics.  But I was forgetting my physics I class.  Voltage is a "potential difference".  If the voltage is the same on both sides of the LED.  The potential difference the LED sees is 0.  Current only flows when there IS potential difference.  It's all coming back to me now... :)

On a related note, I hooked up the LED the other way, so the MCU sinks rather than provides current.  Anyone wanna direct me to those threads that "beat this topic to death"?  I'd like to read up on them.  If the MCU can (heat?)sink as much current as it can provide, what's the advantage to either?