Toggle LED when current is flowing towards GPIO

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I recently created a mod chip from an open source project, and they were toggling 2 LED's with the current flowing towards the GPIO.

It seemed to work with no issues, so I decided to do the same thing in one of my own projects. 

I figured I may as well start with programming the LED before I tackle the USB functionality, but quickly realized I have no idea how to do that.

 

So I opened up the source code for the mod chip, and they have the LED configured as an output, which is now confusing me more than ever.

Surely the magic smoke would appear if I ran current towards an output, right?

It could be possible the source code I have, is for a different version, but I'll let you guys be the judge of that.

 

Anyways, my main question is, how can I toggle an LED, where the anode is connected to VCC, and the cathode to GPIO.

 

Here is the LED code for the modchip, it is literally just toggling the output pin, but it just seems strange to run current towards an output.

 

// green
#define LED1_ON   PORTD &=~0x04
#define LED1_OFF  PORTD |= 0x04
// red
#define LED2_ON   PORTD &=~0x08
#define LED2_OFF  PORTD |= 0x08
#define LED_INIT  DDRD = 0x0C;

 

Here is my circuit, the pin on the modchip is connected virtually the same way, just minus E13.

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That works perfectly.

 

A GPIO pin has two transistors (CMOS FETs). One is Vcc to output and one is output to ground. When the pin is low, you have a transistor to ground and it sinks current very well. There ARE small differences in the characteristics of the PChannel pullup and the NChannel pulldown and you will see that in the spec sheet where the output sink and source capabilities are characterized. 

 

But, fear not, it works fine. The OLD STK500 did this, much to the confusion of many new users. But, electrically, it was no big deal.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah that's great, thank you so much.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

programming the LED before I tackle the USB functionality

The difference would be similar to riding a scooter and driving a spacecraft.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The difference would be similar to riding a scooter and driving a spacecraft.

Or, lifting a glass of water vs competing in the olympics. 

 

As far as your led, the AVR is simply supplying the GND path for the led---that's what happens when the IO output is driven low---conventional current flows into the pin towards gnd.

 

But, fear not, it works fine. The OLD STK500 did this, much to the confusion of many new users. But, electrically, it was no big deal.

I believe the STK used a NPN transistor, to minimize pin loading, but it was the same concept---the gnd was being switched.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think the STK500 had a transistor driver for each of the LEDs on the board so that the micro could run at any voltage, 5V, 3V, even less, and the LEDs always were equally bright, regardless of the operating voltage.

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK on STK500.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

I believe the STK used a NPN transistor, to minimize pin loading, but it was the same concept---the gnd was being switched.

Only the very coolest people would refer to a transistor base as "the grid".  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


 

Don't know about the LEDs being equally bright for different VCC levels, the series resistor is always 150R and the GPIO pin is always carrying the full LED current.

 

The buffer transistors seem to be a waste of space and effort as they are wired up as common base (driven by the emitter), so no current or voltage gain and don't even work as a constant current source. surprise

 

 

Edit maybe VCC is always 5V?? and VTG changes? then that would make a lot of sense.

 

OK apologies to the Atmel STK500 designers cheeky VCC is always 5V so the above circuit makes a lot of sense.

 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Sat. Jun 15, 2019 - 11:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Slightly odd hookup.  The transistors apparently were there so that a hi level (approx VTG) would turn the leds off.  Low (on) would be fine with or without the transistors.  So the AVR supplies all the current to the leds (plus the incidental base current).  They could have just wired them up with the emitter grounded, then a base going high would turn the led on.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:

programming the LED before I tackle the USB functionality

The difference would be similar to riding a scooter and driving a spacecraft.

I think I'll be ok ;), I'm much better at programming than with electronics. Plus Lufa makes things pretty simple.

 

avrcandies wrote:

 

As far as your led, the AVR is simply supplying the GND path for the led---that's what happens when the IO output is driven low---conventional current flows into the pin towards gnd.

 

Ok that makes sense, its actually pretty obvious now that I look at it.

Last Edited: Tue. Jun 18, 2019 - 10:33 PM