Hi all, I'm working on an project for an object-oriented class, and I chose to do my final project in Arduino. However, I'm running into an issue where it seems that all 8 analog pins stop working if I use more than 6 digital pins. While I'm comfortable writing code, I am still very inexperienced when it comes to working with electronics, so any advice would be greatly appreciated.
The analog pins will work if I comment out one of the pinMode lines for the LEDs. It doesn't matter which one is commented out as long as there are no more than 6 pinMode lines for the digital pins. My question is, is this the intended behavior? If not, how can I get full use out of all 22 IO pins on the nano? Thank you in advance!
Code: Arduino Cloud
Circuit Photo and Diagram in another comment below.
Like always we need to see the code, a schematic and a photograph of your wiring.
Please note at three are needed, with a none Fritzing schematic, hand drawn is fine.
Ah you're right! I deleted a lot of code trying to isolate the problem and I must've deleted the other pinMode() calls. Code has been updated with some changes. This is what I get on the Serial monitor now:
... Initializing Component #0 : LED
- 0: Assigned PIN D2 as OUTPUT pin
... Initializing Component #1 : LED
- 1: Assigned PIN D3 as OUTPUT pin
... Initializing Component #2 : LED
- 2: Assigned PIN D7 as OUTPUT pin
... Initializing Component #3 : LED
- 3: Assigned PIN D5 as OUTPUT pin
... Initializing Component #4 : LED
- 4: Assigned PIN D4 as OUTPUT pin
... Initializing Component #5 : LED
- 5: Assigned PIN D6 as OUTPUT pin
... Initializing Component #6 : Button
- 6: Assigned PIN D8 as INPUT pin
... Initializing Component #7 : Motor Driver
- 7: Assigned PIN A4 as OUTPUT pin
... Initializing Component #8 : Motor
- 8: Assigned PIN A5 as OUTPUT pin
- 8: Assigned PIN A6 as OUTPUT pin
- 8: Assigned PIN A7 as OUTPUT pin
... Initializing Component #9 : Motor
- 9: Assigned PIN A3 as OUTPUT pin
- 9: Assigned PIN A2 as OUTPUT pin
- 9: Assigned PIN A1 as OUTPUT pin
writing to motor driver pins
motor pins should be active
However the analog pins still don't work if all 6 LEDs are enabled. If I comment one out then they mysteriously start to work.
I have more resistors, but I am only using one. I have confirmed that the LEDs work properly. The linked code will turn on one LED at a time at the press of the button. Some of the LEDs require 3V and the rest require 2V. (All draw 20mA).
Ultimately the plan is to have the LEDs act as a "View" displaying which "State" is active. The full code (not shown) acts as a state machine and each LED corresponds to a state. Only the current state will have its LED light up so there's no concerns about having multiple LEDS on at the same time.
Sorry, you are right, of course. Even with a 2V LED drop, its close to 28 mA.
Since the previous led is turned off after the next led is turned on, there may be a moment of double current.
But I still cannot see why that should give you trouble only when one led is commented out. I'm assuming that you have tried commenting out one of the other leds and not just the green one.
Is the button being read with an internal pull up resistor?
The equation is fine, it is just your use of it that is wrong.
Each LED needs its own resistor.
I think you are wrong on this. LEDs are what is known as none linear devices. An LED might be rated for 20mA but that does not mean it will draw 20mA, that depends on the resistor and the voltage. Just a small change in the actual voltage across the LED will have a huge change in the current it draws.
Also LEDs will not share current evenly when placed directly in parallel, that is what each LED needs its own resistor.
Get that bit right and you might find it works as expected, although you might have already damaged your Nano.
The LEDs are each connected to a digital pin, and only one digital pin is active at one time. I have a multimeter and have confirmed that the current ranges from 10-25ish mA so there is no problem with the LEDs. Also as stated before, the analog pins work just fine if there are only 5 calls to pinMode for the digital pins.
Also in parallel they would merely have a lower current, and thus there is no risk of going over on the voltage. If anything the LEDs would just be dimmer.
Embarassingly enough, I did not actually test commenting out the other LEDs in this most recent iteration. I'm not sure what changed from past iterations, but as long as PIN D7 isn't being used, the analog pins work fine. This is still a bit troubling as I was hoping to be able to use all 22 pins but I can definitely proceed forward with one less LED.