Trains.com

Subscriber & Member Login

Login, or register today to interact in our online community, comment on articles, receive our newsletter, manage your account online and more!

Using Ardunio can you use multiple LED's with same terminals?

2021 views
21 replies
1 rating 2 rating 3 rating 4 rating 5 rating
  • Member since
    July 2009
  • From: lavale, md
  • 4,678 posts
Posted by gregc on Friday, August 21, 2020 6:34 PM

Harold

this is not the appropriate forum to discuss generic arduino programming.

either contact me via a message or post your questions on the arduino programming questions forum.

greg - Philadelphia & Reading / Reading

  • Member since
    January 2019
  • 158 posts
Posted by wolfman hal on Friday, August 21, 2020 5:43 PM

I am getting an error message for the following

#define blueLed 2

Error message reads:

Expected ' ) ' before numeric constant. What am I doing wrong?

Harold

  • Member since
    January 2009
  • From: Bakersfield, CA 93308
  • 6,526 posts
Posted by RR_Mel on Friday, August 21, 2020 2:32 PM

I use all 20 I/O pins for outputs on the UNO for my random lighting controller.  As I use incandescent bulbs that draw well over the Arduino max I built a high current output expansion module using 7 channel driver chips.



The inputs can be used for outputs too.

By using the driver chips I can use a higher voltage to operate the bulbs without dinging the Arduino.  I use a DC to DC Buck Converter to reduce 12 volts to 8½ volts.  The 12 volt Grain of Wheat bulbs draw between 70ma and 100ma each, I set the bulb voltage to 8½ volts for more realistic 1950s lighting and increased bulb life. 


Mel


 
My Model Railroad  
http://melvineperry.blogspot.com/
 
Bakersfield, California
 
I'm beginning to realize that aging is not for wimps.

  • Member since
    February 2002
  • From: Reading, PA
  • 30,002 posts
Posted by rrinker on Friday, August 21, 2020 1:19 PM

wolfman hal

My era is still black and white TV sets. Thanks Greg ,I took my first tutorial on Millis.

In the tutorials they use the Anolog pins.  Is there a reason?

Harold

 

 If they are simply turning the pin on and off - the analog pins work as digital outs just fine. If the circuit needs to use PWM output to simulate a varying voltage, then only certain pins can be used, only some support hardware PWM (it's not the analog pins - they are the analog pins because they support analog INPUT). 

 The analog pins can also be used for digital inputs with no problem - like a pushbutton. 

                             --Randy

 


Modeling the Reading Railroad in the 1950's

 

Visit my web site at www.readingeastpenn.com for construction updates, DCC Info, and more.

  • Member since
    July 2009
  • From: lavale, md
  • 4,678 posts
Posted by gregc on Friday, August 21, 2020 11:48 AM

wolfman hal
In the tutorials they use the Anolog pins.  Is there a reason?

i don't believe it matters.   post a link to the tutorial

does the code I posted make sense to you?

greg - Philadelphia & Reading / Reading

  • Member since
    January 2019
  • 158 posts
Posted by wolfman hal on Friday, August 21, 2020 10:46 AM

My era is still black and white TV sets. Thanks Greg ,I took my first tutorial on Millis.

In the tutorials they use the Anolog pins.  Is there a reason?

Harold

  • Member since
    February 2002
  • From: Reading, PA
  • 30,002 posts
Posted by rrinker on Thursday, August 20, 2020 5:57 PM

 Search for Geoff Bunza Harrys TV shop - slightly more modern COLOR TV simulation using those little OLED displays you cna get for Arduinos. There's an article on how to go about building it as well as a YouTube video of it working - pretty neat, but too new for my layout.

 Geoff's B&W TV flicker randomizes the delay around a range so it looks more like the variation between a program and the commercials and so forth. It's a bit of a dig to get the code, and it can't be linked here because it requires linking a competing magazine's web site. But a little searching will get you pointed to the right place. I was pretty sure I had the file with the sample code in it, but I can't find it, or I'd paste the bit that controls the randomizing of the delay.

                                           --Randy

 


Modeling the Reading Railroad in the 1950's

 

Visit my web site at www.readingeastpenn.com for construction updates, DCC Info, and more.

  • Member since
    July 2009
  • From: lavale, md
  • 4,678 posts
Posted by gregc on Thursday, August 20, 2020 1:52 PM

use millis() to capture the number of milliseconds since the start of the program.   perform some action when it exceeds some timestamp and reset the timestamp to the current value of milliseconds

have separate functions for your two separate activities, each using msec independently.

each function simply toggle an LED, if on turn it off, if off, turn it on (! digitalRead (pin))

change the led pins to what you are using.  i picked values from my hardware

 

#define yellowLed  10
#define blueLed    11
#define whiteLed   12

unsigned long msec;

// -----------------------------------------------------------------------------
void
setup (void)
{
    pinMode (yellowLed, OUTPUT);
    pinMode (blueLed, OUTPUT);
    pinMode (whiteLed, OUTPUT);

    digitalWrite (blueLed,  HIGH);
    digitalWrite (whiteLed, LOW);
}

// -----------------------------------------------------------------------------
void
tv (void)
{
    static unsigned long msecLst = 0;

#define TvPeriod  20
    if (msec - msecLst > TvPeriod)  {
        msecLst = msec;

        digitalWrite (blueLed,  ! digitalRead (blueLed));
        digitalWrite (whiteLed, ! digitalRead (whiteLed));
    }
}

// -----------------------------------------------------------------------------
void
trafficLight (void)
{
    static unsigned long msecLst = 0;

#define TlPeriod  500
    if (msec - msecLst > TlPeriod)  {
        msecLst = msec;

        digitalWrite (yellowLed, ! digitalRead (yellowLed));
    }
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    msec    = millis();

    trafficLight ();
    tv ();
}

 

please message ("start conversation") me with questions

greg - Philadelphia & Reading / Reading

  • Member since
    September 2003
  • 21,669 posts
Posted by Overmod on Thursday, August 20, 2020 1:39 PM

wolfman hal
simple.  I had a project for a TV with Blue & White LED's lights blinking fast. (FLICKERING)

I am tempted to tell you to model the ACTUAL sort of television flicker -- which is 'modulated' by the program, much brighter during the ads -- perhaps by reading some analog source like a video or audio signal and driving amplitude of the LEDs proportionally -- like those screen saver 'visualizers' Apple provided.  (Don't forget that in this age of sound decoders, TV watchers have a soundtrack, too, that you could probably crib from YouTube nostalgia videos...)

I think gregc ought to walk you through his code (not just as if fully commenting it but explaining his syntax conventions and internal algorithmic logic) as none of this code is particularly intuitive even to people familiar with older 'consumer' programming approaches.

  • Member since
    January 2019
  • 158 posts
Posted by wolfman hal on Thursday, August 20, 2020 1:24 PM

Please see if you can give me the code for something simple.  I had a project for a TV with Blue & White LED's lights blinking fast. (FLICKERING)

void loop() {                     // Yellow Blicking Traffic Light

digitalWrite(yellowLED,HIGH);

delay(500);

digitalWrite(yellowLED,LOW);

delay(500); 

 

digitalWrite(blueLED,HIGH);// TV

delay(20);

digitalWrite(blueLED,LOW);

 

digitalWrite(whiteLED,HIGH);

delay(20);

digitalWrite(whiteLED,LOW);

The TV portion started out as a stand alone project. With the code above the blinking was FLICKERING and looked good. Because I found out yesterday from you guys that Ardunio only can run the one program you download at a time. I combined the TV project with the YELLOW BLINKING TRAFFIC LIGHT.

The TV project is Blinking at a much slower rate. I have reduced the Delay rate but still can not get it back to flicker. I assume this is because the Blinking light traffic light is now at work.  I intend to add more projects such as lights in different rooms at the farm house. I have a feeling this is going to make the situation worse. I really am not yet at the stage where I understand Greg's code. Is there any simple tutorials on this type of code.(BASIC). I don't want to just start copying code with no idea of what I am dong or why.

Thanks in Advance.  Stay SAFE

Harold

  • Member since
    July 2009
  • From: lavale, md
  • 4,678 posts
Posted by gregc on Thursday, August 20, 2020 8:26 AM
    static unsigned long msecLst = 0;
           unsigned long msec    = millis();

#define Period  200
    if (msec - msecLst > Period)  {
        msecLst = msec;

        digitalWrite (pin, ! digitalRead (pin));
    }

 

greg - Philadelphia & Reading / Reading

  • Member since
    February 2002
  • From: Reading, PA
  • 30,002 posts
Posted by rrinker on Thursday, August 20, 2020 7:53 AM

 There's actually a library that makes that easy - called Metro (for metronome I suppose, not the game or as a reference to the area around a city). That easily lets you run multiple things each with their own intervals. I use it in my turnout controller so it can monitor the local buttons, the remote inputs, and move the servos all at the same time.

 The repository for it will tell you that it's old and point you to a replacement, which has more parameters (which for most of these purposes just makes it more complicated) so I've stuck with the Metro library.

Once the library is installed, in addition to your pin definitions and other setup, you declare a Metro object with the desired delay (this comes BEFORE the Setup() function):

Metro hbMetro(750)

That creates a Metro object called hbMetro with a trigger time of 750ms

In your main Loop(), you just encapsulate everything in an If conditional:

if (hbMetro.check() == 1) {

   (stuff you want to happen every 750ms)

}

For example, in my servo controller, I use this to blink a heartbeat LED so I know the program is running. The full code is:

if (hbMetro.check() == 1) {   // Heartbeat LED

    if (hbstate == HIGH)  hbstate = LOW;

    else hbstate = HIGH;

    digitalWrite(HBLED, hbstate);

 

  }

Every 750ms, it simply turns the LED on if it was off, or turns it off if it was on.

The obvious (or maybe not so obvious) thing is that the code inside the if block cannot take longer than the time interval to actually execute or the timing will be messed up. Arduino and ATMega328 micros do not do preemptive multitasking like say Windows does - if one block takes a long time to execute, any following blocks with different intervals will not happen on time. 

 Note there is no delay() used - that's because what's inside the if only runs once every 750ms. If 750ms have not elapsed since the last time it ran, the if fails and the code drops to the next section, which in my program is another if with a different Metro object being checked.

 Getting away from timing a flash or blink by using delay() is probably a difficult concept for a newcomer to programming to grasp. 

 This is a link to the replacement library, called Chrono. It has examples on how to use it:

http://sofapirate.github.io/Chrono/

The main difference is that in Chrono you don't create the object with a desired delay value, you just check if the desired number of milliseconds have passed. With Chrono my code would look like this:

Chrono hbChrono;   //to create the object, at the top

Then is the loop it would be

if (hbChrono.hasPassed(750)) {   // Heartbeat LED

   hbChrono.restart();

    if (hbstate == HIGH)  hbstate = LOW;

    else hbstate = HIGH;

    digitalWrite(HBLED, hbstate);

 

 

  }

The main real difference being that you have to restart the Chrono each time. With the Metro library, you only need to restart it if you change the interval after the fact - you set an interval when you declare it, but you can later adjust the interval if you need to start checking something more or less frequently. 

                                   --Randy

 


Modeling the Reading Railroad in the 1950's

 

Visit my web site at www.readingeastpenn.com for construction updates, DCC Info, and more.

  • Member since
    January 2009
  • From: Bakersfield, CA 93308
  • 6,526 posts
Posted by RR_Mel on Thursday, August 20, 2020 7:07 AM

The Arduino will not multitask easily if at all using delay.

https://melvineperry.blogspot.com/2019/01/january-16-2019-arduino-multasking.html

https://www.youtube.com/watch?v=Myz6oiG2Vyk

 

Mel



 
My Model Railroad  
http://melvineperry.blogspot.com/
 
Bakersfield, California
 
I'm beginning to realize that aging is not for wimps.
 

  • Member since
    January 2019
  • 158 posts
Posted by wolfman hal on Thursday, August 20, 2020 6:44 AM

OK I understand now. So for the beginers like me. You have to combine all project into one script.  I plan on about 10 projects so it is going to become one big script.  And yes I will watch the MA for the total.

Thanks

Stay Safe

Harold

  • Member since
    July 2009
  • From: lavale, md
  • 4,678 posts
Posted by gregc on Wednesday, August 19, 2020 7:33 PM

personal message.

click my picture and click "start conversation" under "Add as Friend" to the right of my picture on my profile page

reminder to some of you to check your messages

 

a single program can control all the outputs.

greg - Philadelphia & Reading / Reading

  • Member since
    January 2019
  • 158 posts
Posted by wolfman hal on Wednesday, August 19, 2020 7:28 PM

Sorry greg.  What is a PM?

The project are two simple ones

1. A blinking yellow traffic light in town

2. A flickering Blue and White LED going in the farmhouse to simulate a TV Set.

 

What good are all the outputs on the Ardunio if it will only operate one at a time?

 

Harold

  • Member since
    July 2009
  • From: lavale, md
  • 4,678 posts
Posted by gregc on Wednesday, August 19, 2020 5:20 PM

the arduino can only run one program ("project") at a time.

sounds like you need to combine the two projects.

send me a PM with a copy of both projects and I can help

greg - Philadelphia & Reading / Reading

  • Member since
    January 2019
  • 158 posts
Posted by wolfman hal on Wednesday, August 19, 2020 4:55 PM

I just finished working on two different projects on the breadboard connected to the Ardunio. Both projects work fine. When I download one the other goes off. 

How do I fix this?

Harold

  • Member since
    February 2002
  • From: Reading, PA
  • 30,002 posts
Posted by rrinker on Wednesday, August 19, 2020 9:13 AM

 The limit of the microcontroller used in the Arduino Uno/Nano is 40ma per pin, with a total maximum of 200ma for the entire chip - so you can;t just load each pin up to 40ma.

 The small SMD LEDs are plenty bright with 5ma or less, so you could potentially put 2 or 4 in parallel. They may not light equally bright, but if they are different structures not next to one another, this might not matter. Series is best, but with white LEDs, you'd be lucky to get 2 in series to work with a 5 volt supply. If you really want to drive a lot of LEDs with one Arduino, look to specific LED driver boards or examples of a simple circuit as Greg says. That way, the LED supply can be a higher voltage, allowing more LEDs in series, the limit there being the sum of the voltages of each LED versus the supply voltage fo the circuit - those LED strips typically link 3 LEDs in series with a single resistor and run on 12 volts. 4 LEDs would be too much for 12V. Each group of 3 LEDs is wired in parallel with each of the other groups of 3 LEDs to make the tape. This takes several amps for a full reel. 

                                      --Randy

 


Modeling the Reading Railroad in the 1950's

 

Visit my web site at www.readingeastpenn.com for construction updates, DCC Info, and more.

  • Member since
    January 2009
  • From: Bakersfield, CA 93308
  • 6,526 posts
Posted by RR_Mel on Wednesday, August 19, 2020 9:11 AM

The Arduino is a slick device but you must limit each output to a Max of 40ma!!!!

I make an expansion modules for voltages higher than 5 volts and currents higher than 40ma to drive my goodies.

There are blank expansion boards available for the Arduino UNOs and MEGAs on eBay.

I make expansion boards for the NANO.



I keep the Arduino outputs to under 30 ma to be on the safe side.

The best driver chips (7 channel/output) I’ve found are the ULN2003 (inverting) and TD62304 (non inverting), they will sink 500 ma up to 50 volts per output.

For single circuit drivers I use a Darlington Transistor, MPSA13, it will sink 500ma up to 30 volts.







Mel


 
My Model Railroad  
http://melvineperry.blogspot.com/
 
Bakersfield, California
 
I'm beginning to realize that aging is not for wimps.
 

  • Member since
    July 2009
  • From: lavale, md
  • 4,678 posts
Posted by gregc on Wednesday, August 19, 2020 7:54 AM

because the LED current varies significantly with voltage, two LEDs in parallel may not be the same intensity even if the resistor is halved.    LEDs would draw equal current if wired in series.

there are inexpensive 10/20 ma regulator designed specifically for LEDs.   depending on the supply voltage, one or more LEDs can be wired in series, such as for one structure. 

an arduino can drive an external transistor, pulling an LED circuit to ground to turn on/off LEDs wired in series with a regulator possibly powered by 12V which is higher that the arduino supply voltage.

greg - Philadelphia & Reading / Reading

  • Member since
    January 2019
  • 158 posts
Using Ardunio can you use multiple LED's with same terminals?
Posted by wolfman hal on Wednesday, August 19, 2020 7:04 AM

I am new to Ardunio.  I have created some circuits on the breadboard and am now ready to start on my layout. I plan on going from the Ardunio to a terminal strip or strips and then to the projects. Using the example below 

Example: Two seperate building away from each other. Same brightness and same on and off rate. Another words same circuit.

My question is this.

1. Once I build the first circuit on the terminal strips can I add the second LED for the other building to the same terminal posts after the resistor and the other side to the ground post?

If this works how many LED's can I use?

Subscriber & Member Login

Login, or register today to interact in our online community, comment on articles, receive our newsletter, manage your account online and more!

Users Online

There are no community member online

Search the Community

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
Model Railroader Newsletter See all
Sign up for our FREE e-newsletter and get model railroad news in your inbox!