Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Another LED "Hello world!" program


  • Please log in to reply
11 replies to this topic

#1 Stanislav Husár

Stanislav Husár

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 14 July 2011 - 09:37 AM

Hi,

Maybe you remember my topic "LEDs and potentiometer" in Netduino forum.Today I got my Netduino and did testing.
Program had somehow strangely incorrect behaviour at pin D13.I modified and simplified one of its methods - SetPort.
Now the program behaves correctly.

Program description:My program goes through all digital GPIO ports. At each time, one is ON and others are OFF.
This way the LEDs blink.Time delay between two neighbouring pins turned ON can be adjusted by potmeter at pin A0
varying from no delay to 1024 milliseconds.

Circuit description: Digital GPIO ports are connected to NPN transistor(2N2222A) base throught about 10K resistor.
I have bought another value, I do not know which, but 10K will work.
LEDs are connected from 5V supply on Netduino to transistor collector, throught 200 Ohm resistor.
Transistor emittor is connected directly to Ground.

Analog pin A0 is connected to potmeter's center pin.Side pins of potmeter are connected to (3,3V and AREF) and to Ground.Recommended value for potmeter is 10KOhms.

Required parts: 14x LED, 14x 200Ohm resistor, 14x 10KOhm resistor,
14x 2N2222A transistor, Netduino or Netduino Plus and wiring.

Code:
/*  Programmed by Stanislav Husár.
 *  Connect all digital IO ports to 14 separate LEDs and a potentiometer to port A0.
 *  This program goes throught each of LEDs in forward direction turning them on,
 *  while turning other LEDs off. After each cycle, program simply starts again
 *  at first LED, looping animation forever. Animation speed can be controlled
 *  by potentiometer at A0 port.
 *  
 *  Update: Circuit tested  at 14th July 2011. Pin D13 had incorrect behaviour,
 *  so I modified SetPort method. Meanwhile I discovered, that it can be simplified.s
 */
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoTest
{
    public class Program
    {
        
        static OutputPort[] ports;//LED ports
        
        //Pins of LED ports
        static Cpu.Pin[] pins = new Cpu.Pin[] { Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1, Pins.GPIO_PIN_D2, Pins.GPIO_PIN_D3, Pins.GPIO_PIN_D4, Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D7, Pins.GPIO_PIN_D8, Pins.GPIO_PIN_D9, Pins.GPIO_PIN_D10, Pins.GPIO_PIN_D11, Pins.GPIO_PIN_D12, Pins.GPIO_PIN_D13 };

        //Potentiometer port
        static AnalogInput speed;


        public static void Main()
        {
            //Initialize ports
            speed = new AnalogInput(Pins.GPIO_PIN_A0);
            ports = new OutputPort[pins.Length];
            for (int x = 0; x < pins.Length; x++)
                ports[x] = new OutputPort(pins[x], false);

            int port = 0;//Index(into ports array) of active pin.

            while (true)//repeat forever
            {
                SetPort(port);

                port = (port + 1) % ports.Length;//Add 1 to port id and clamp value to last index in ports array..
                Thread.Sleep(speed.Read() + 1);
            }
        }
        /*
 		* Sets specified pin to high, turning LED on.Also sets other pins to low, turning other LEDs off.
 		* WARNING: id parameter must be inside range of ports array.Not tested, so putting an out-of-range value will cause exception at runtime.
 		*/
        public static void SetPort(int id)
        {
            for (int i = 0; i < ports.Length; i++)
                ports[i].Write(id == i);
        }

    }
}

Thanks to Stefan and Mario Vernari.You two helped me a lot to design circuit.


http://www.youtube.com/watch?v=sZ-ZRO2xJRo


#2 Stuart Crawshaw

Stuart Crawshaw

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts
  • LocationLeeds, United Kingdom

Posted 14 July 2011 - 11:39 AM

Awesome, Just for my knowledge (not a critisism or anything) but could you of done this without the need for all of the transistors and resistors or was this just so you could create a circuit with an example of how a transistor could be used? Also, if the transisors were not used, could you have 1 resistor going to ground with and have that resistor as a common "gate" to ground for all of your LED's? Like i said this is not a critisism, i think your creation is an awesome little bit of electronics, im just trying to better my understanding of things (i am more of a code junkie than an electronics kinda guy). thanks,
Intelligent People Aren't Afraid To Ask For Help.

#3 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 14 July 2011 - 11:51 AM

Stuart, I'd like be Stanislav to answer you! Cheers
Biggest fault of Netduino? It runs by electricity.

#4 Stanislav Husár

Stanislav Husár

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 14 July 2011 - 08:38 PM

Awesome,

Just for my knowledge (not a critisism or anything) but could you of done this without the need for all of the transistors and resistors or was this just so you could create a circuit with an example of how a transistor could be used?

Also, if the transisors were not used, could you have 1 resistor going to ground with and have that resistor as a common "gate" to ground for all of your LED's?

Like i said this is not a critisism, i think your creation is an awesome little bit of electronics, im just trying to better my understanding of things (i am more of a code junkie than an electronics kinda guy).

thanks,

Hi Stuart,

Transistors are there because GPIO pins on Netduino have 8mA limit, which is not enough to power a LED.
But it is enough to close NPN transistor.Transistors allow higher current to flow between Collector and Emittor, enough to drive LEDs.

Anyway, whether you use transistors or not, you may not use one resistor near more LEDs.
Resistor limits the current that flows in the circuit.But resistor near the LED does not need to limit current, that flows through one LED.
Using suggested circuitry, it will limit current flowing through all LEDs together and then through it.
This will change current, that flows through the LEDs, and also the current will vary depending on count of LEDs turned on.

#5 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 15 July 2011 - 04:06 AM

Transistors are there because GPIO pins on Netduino have 8mA limit, which is not enough to power a LED.
But it is enough to close NPN transistor.Transistors allow higher current to flow between Collector and Emittor, enough to drive LEDs.

Excellent!
The transistor is a current amplifier.

Anyway, whether you use transistors or not, you may not use one resistor near more LEDs.

Yes and no.
If you light up one led at once there's no need for many resistor. When the other leds are off it's like they are disconnected from the circuit.
However, when your program wants to light 2+ leds, it's better to consider one resistor per led. (read further)

Resistor limits the current that flows in the circuit.But resistor near the LED does not need to limit current, that flows through one LED.

Current is much like water.
When a pipe limit the water flowing in it, then the water will be limited in the rest of the pipes too.
If you place just one resistor instead of many, the current is MUCH MORE limited. In this case, when you try to light TWO leds -for example- there's NO more current flowing from the supply, NOR MORE through the leds. The current is always the same as one led only, but it splits to each led, half and half. So, if you use ONE resistor for many leds, you may light more than one, but the more you light the less they shine. That's because the current is always the same and be "shared" among all the led involved in the lighting.

Cheers
Biggest fault of Netduino? It runs by electricity.

#6 Stuart Crawshaw

Stuart Crawshaw

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts
  • LocationLeeds, United Kingdom

Posted 15 July 2011 - 08:48 AM

Transistors are there because GPIO pins on Netduino have 8mA limit, which is not enough to power a LED.


I did not realise this, thank you for the information. Although i have plugged an LED in series with a resistor on one of the GPIO pins and it worked, Could this be drawing more current than it should?

If you light up one led at once there's no need for many resistor. When the other leds are off it's like they are disconnected from the circuit.
However, when your program wants to light 2+ leds, it's better to consider one resistor per led. (read further)


Thats what i was trying to get at, The video and description shows only one LED on at a time so i though it would be easier to use just the one resistor.

So, if you use ONE resistor for many leds, you may light more than one, but the more you light the less they shine. That's because the current is always the same and be "shared" among all the led involved in the lighting.


This in theory could make a good effect (if done quick enough) of the LED's fading in/out and the next one it lit and the other go off?


Thanks Guys and thanks for the corrections to my understanding of this circuit.
Intelligent People Aren't Afraid To Ask For Help.

#7 Stanislav Husár

Stanislav Husár

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 15 July 2011 - 09:23 AM

Thats what i was trying to get at, The video and description shows only one LED on at a time so i though it would be easier to use just the one resistor.

It has only one LED on at at time, but you can modify the program to other animations which can have more LEDs on at a time.


This in theory could make a good effect (if done quick enough) of the LED's fading in/out and the next one it lit and the other go off?

This can be made by PWM, use PWM output of netduino with PWM class, or quickly turn on and off the pins simulating PWM.
I will try to create more stepper animation, on success I can post program here.

#8 Stuart Crawshaw

Stuart Crawshaw

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts
  • LocationLeeds, United Kingdom

Posted 15 July 2011 - 09:27 AM

Cool, I look forward to seeing the outcome :)
Intelligent People Aren't Afraid To Ask For Help.

#9 Stanislav Husár

Stanislav Husár

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 15 July 2011 - 09:55 AM

So I did it! Program is now simulating PWM.As a prototype, I controlled pulse width (or what it is) by potmeter, but it was useless, because animation was then either too quick, or simulated PWM looked ugly.I hardcoded it to about 20 ms.

Circuit changes: Potmeter is no longer needed, and so AREF can be floating(nothing wired to it).3V3 and A0 are also floating.

Program changes: Program again goes through all LEDs, but now they fade in and out.

New code:
/*  Programmed by Stanislav Husár.
 *  Connect all digital IO ports to 14 separate LEDs and a potentiometer to port A0.
 *  This program goes throught each of LEDs in forward direction turning them on,
 *  while turning other LEDs off. After each cycle, program simply starts again
 *  at first LED, looping animation forever. Animation speed can be controlled
 *  by potentiometer at A0 port.
 *  
 *  Update 2: PWM simulation, pulse width is hardcoded to 20ms.
 */
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoTest
{
    public class Program
    {

        static OutputPort[] ports;//LED ports

        //Pins of LED ports
        static Cpu.Pin[] pins = new Cpu.Pin[] { Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1, Pins.GPIO_PIN_D2, Pins.GPIO_PIN_D3, Pins.GPIO_PIN_D4, Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D7, Pins.GPIO_PIN_D8, Pins.GPIO_PIN_D9, Pins.GPIO_PIN_D10, Pins.GPIO_PIN_D11, Pins.GPIO_PIN_D12, Pins.GPIO_PIN_D13 };

        

        public static void Main()
        {
            
            ports = new OutputPort[pins.Length];
            for (int x = 0; x < pins.Length; x++)
                ports[x] = new OutputPort(pins[x], false);

            int fO = 0;
            int fI = 1;
            while (true)
            {
                for (int x = 0; x < 20; x++)
                {
                    ports[fO].Write(false);
                    ports[fI].Write(true);
                    Thread.Sleep(x);
                    ports[fO].Write(true);
                    ports[fI].Write(false);
                    Thread.Sleep(20 - x);
                }
                ports[fO].Write(false);
                ports[fI].Write(true);
                fO = (fO + 1) % 14;
                fI = (fI + 1) % 14;
            }
            
        }
    }
}


#10 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 16 July 2011 - 07:35 AM

so AREF can be floating(nothing wired to it).3V3 and A0 are also floating.

*** Never ever leave inputs floating! ***
Tie them with a medium value resistor (e.g. 10K) to the +3.3V or ground (much better whenever possible).
The resistor isn't mandatory, but prevents any stress/damage in case you accidentally set an input (connected to the ground) to an output set to "true".
Unused I/O ports should be set as outputs (then "false"), then left without anything connected to. That doesn't require any extra power.
Cheers
Biggest fault of Netduino? It runs by electricity.

#11 Stanislav Husár

Stanislav Husár

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 16 July 2011 - 08:15 AM

*** Never ever leave inputs floating! ***
Tie them with a medium value resistor (e.g. 10K) to the +3.3V or ground (much better whenever possible).
The resistor isn't mandatory, but prevents any stress/damage in case you accidentally set an input (connected to the ground) to an output set to "true".
Unused I/O ports should be set as outputs (then "false"), then left without anything connected to. That doesn't require any extra power.
Cheers

I do not undestand you.Is it bad to just not connect anything to specified pins(AREF, A0)?

#12 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 16 July 2011 - 08:36 AM

From the AVR ATMega328 specs (page 81):
If some pins are unused, it is recommended to ensure that these pins have a defined level. Even though most of the digital inputs are disabled in the deep sleep modes as described above, floating inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (Reset, Active mode and Idle mode).
The simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up.
In this case, the pull-up will be disabled during reset. If low power consumption during reset is important, it is recommended to use an external pull-up or pull-down. Connecting unused pins
directly to VCC or GND is not recommended, since this may cause excessive currents if the pin is accidentally configured as an output.

That's *NOT* the Netduino processor, but it's a generic rule for any digital circuit.

For the AT91SAM processor (the Netduino's one), there's no explicitly stated, but there's some people on internet wondering on what to do. The processor itself offers the pullup resistors by default: in this case the pin could be left unconnected. However the analog ports have *NOT* any pullup, so should be grounded (via resistor).
Note: you won't blow your Netduino leaving the pins unconnected. It's only a good practice to bear in mind. I was not blame you at all!!! Instead, I am happy that you enjoy playing with circuits!
Cheers
Biggest fault of Netduino? It runs by electricity.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.