Hi folks
this is my first forum entry so hello to all.
As an introduction I am a bit of an electronics dumbo although reasonably good at programming.
I have just bought an Arduino Mega for its extra pins and more memory for a project initially requiring 20 output LEDs (10*R+G) driven by 10 input switches for a model railway control system.
The switches I have chosen are SPST and I have them defined as INPUT_PULLUP.
I am currently using the USB port to drive the system but coud switch to a 9V power supply if required although this is already driving several servos.
I have been trying to find out the maximum power for all outputs from the Mega but it all seems quite complex.
My questions are:
Is it safe to use the switches in this way - I have read that an INPUT_PULLUP connected to GND can provide a current. I appear to have just connected the +5 to GND without any intervening circuit.
If each LED has just under 20ma (the LEDs have 330 ohm resistors inline)how many LEDs will the Mega be able to handle? The next phase will require 40 - plenty of pins but enough power?
Even if you are driving one servo you need something more capable than USB or a 9V battery for power,
and you shouldn't be powering a servo from the 5V logic supply at all.
the switch grounds the pin, not the +5V rail.
You have to check the datasheet for details of this - there is a limit overall and possible limits by groups
of pins. Often you can drive more if some are active high and others are active low, and wire some LEDS to
Vcc and some to ground, so that the current is shared between the two rails.
Modern LEDs are very bright, you might not need 20mA at all, note.
Thanks for the answer to question 1, resolves my confusion nicely.
Apologies if question 2 unclear - The servos are not driven by the Arduino and already work fine on the layout.
I only mentioned it as a possibility as a shared supply for the Arduino although probably not a good idea.
I have tried reading anything I can find. The Arduino website only gives mA per pin. I even tried looking at the summary datasheet for the ATmega2560 chip but I can't find details of any overall limit(s). Perhaps my lack of electronics knowledge is stopping me seeing the obvious?
An online LED calculator I found earlier today says the current setup will use about 333mA so should be ok. wth the usb input but not if I add more outputs in the future. Hence the question.
Thanks a lot Mark. I downloaded it and I think I understand a bit better.
There are 4 GND pins on the Mega so looks like the total power available is 800mA and should be ample for what I need but I will have to redesign the switch board to use mutiple GND feeds instead of one and be careful which pins I stick the leads in as per the port limitations NOT just continuously in sets of three as I was originally planning.
Biscit42:
Thanks a lot Mark. I downloaded it and I think I understand a bit better.
There are 4 GND pins on the Mega so looks like the total power available is 800mA and should be ample for what I need but I will have to redesign the switch board to use mutiple GND feeds instead of one and be careful which pins I stick the leads in as per the port limitations NOT just continuously in sets of three as I was originally planning.
Mike
Hmm, not sure if that's a total for all the pins, or per pin - I'd err on the side of caution perhaps? Or at least
monitor the chip temperature if you try it.
40*20mA LED = 800mA.
Too much for a Mega chip (400mA absolute max) and too much for the Mega supply.
Problems like this are usually solved with port expanders.
20mA is high for modern LEDs. Most are alread bright enough with 3-10mA.
Leo..
This subject does seem to be rather complex as I first thought so given Mark and Leo's latest comments I realised I needed to know the LED current that I am using.
So, I decided to use a sacrificial UNO just in case, connected all 10 LEDs (only 5 switch/LED sets are currently soldered into the switchboard) and wrote some code to turn them all on at the same time in setup.
I dug out my old multimeter and read up online how to measure current.
I connected the probes between the Arduino GND and my swithboard and the result was very encouraging.
As Leo implied the multimeter only showed 69.6ma for all 10.
This should mean that whatever the limit of 200mA refers to, I should be able to attach 20 LEDs at the same time when I extend to 10 switch sets in the future.
Apologies to Mark, I could have done this after his first reply but sometimes the old brain doesn't see things and without the experience I needed more of a push.
Is there a board etiquette way of thanking responders?
Frankly, if there is any concern regarding powering multiple LEDs, a MEGA 2560 is not the way to go. A Nano will be quite sufficient.
Where higher power/ higher voltage is required, the best approach is to use TPIC6x595 drivers to control eight loads each. These take three pins to drive and may be chained so three pins control many TPIC6x595 drivers. There are other LED driver chips, but these are cheap, readily available (eBay) and easy to use. You of course require the current setting resistors.
But for indicator LEDs (do you really need 20 mA each? Current LEDs are very bright), a MAX7219 can drive sixty-four (or 32 red-green combo LEDs if that is what you need) per chip, again chain-able on only three processor pins.
Given that it involves your own wiring for the LEDs in any case, the sensible way to approach this is to buy a couple (just in case) of the rubbish MAX7219 matrix modules - as a kit - and assemble them without the actual matrix and sockets for same, using the PCB to wire to your own set of LEDs.
This gives you ridiculous available brightness (but totally controllable both as the maximum which is set by the single resistor and overall in steps by code) requiring no code to continuously multiplex the display; you only update the MAX7219s when you want to change the display.
And why did I call the modules "rubbish"? Because using these modules with the original matrix displays makes them entirely impractical to stack which is what most people want to do with them - if you wish to do this you would use the proper design instead.
Now as to the buttons, you can also matrix those onto a small number of pins but I suspect it may be more practical on a model railway layout, to have one side of each switch grounded. So you would use one input pin per switch and if you required more pins, use another shift register (74HC165) to add eight inputs at a time.
I was wondering what an Arduino had to do with this in the first place as you do not need any electronic logic components at all to switch between red and green LED, but I presume you are making a section control system.
your suggestions are all valid but the problem is that after doing a few small projects using the Uno I decided I needed something bigger - more pins, more memory etc. After much looking at the alternatives which fitted with my current knowledge I chose the Mega. It was only when I had bought it from Italy that I realised I could join the forum in order to ask questions if I needed to. Maybe the direction would have been different if I'd done it the other way round.
My usage is to create a "mimic" panel which communicates with the main controller using code already running on 2 Unos, processes the locking requirements for the traffic flows (Automatic under central control and Manual under signalman control) indicating the current status and requirements for action using the LEDs.
There is an absolute maximum of 10 traffic flows so the Mega should be able to handle all this.
I am using my version of the state/timer code from the forums tutorial section which I found very useful.
I prefer the programming side of things and the external stuff of soldering wires and building boxes etc is a bit of a chore. I managed to solder all the SPST switches to a single strand of old copper wire as a bus and connect it to gnd. The switches code uses INPUT_PULLUP to simplify things. The LEDs are also grounded to the same bus.
I daresay I will be asking more questions in the programming section when I get to trying to make elegant code using Classes and Constructors.