In the process of creating an activity board for my kid, and I started to tinker a bit with Arduino in Tinkercad. The basic idea of the diagram below would be to simpy connect 4 push button to 4 LEDS (as depicted now, simply using LEDS (ignore the lack or resistors), but later using 12v bulbs connected through a relais). Before I get there, I first wanted to get this basic code working.
if I run the Tinkercad simulation, I am puzzled why the red led is on by default, and the other leds work as expected. All four pull down resistors are configured at 1000 Ohm... Any help would be appreciated!
// Define macros //
///////////////////
//CONSTANTS
// inputs
const int buttonred_Pin = 0;
const int buttongreen_Pin = 1;
const int buttonblue_Pin = 2;
const int buttonyellow_Pin = 3;
// outputs
const int relaisred_Pin = 13;
const int relaisgreen_Pin = 12;
const int relaisblue_Pin = 11;
const int relaisyellow_Pin = 10;
// VARIABLES
//inputs
int buttonred_State = 0;
int buttongreen_State = 0;
int buttonblue_State = 0;
int buttonyellow_State = 0;
// initialize pins as outputs / inputs //
////////////////////////////////////////
void setup() {
//inputs
pinMode(buttonred_Pin, INPUT);
pinMode(buttongreen_Pin, INPUT);
pinMode(buttonblue_Pin, INPUT);
pinMode(buttonyellow_Pin, INPUT);
//outputs
pinMode(relaisred_Pin, OUTPUT);
pinMode(relaisgreen_Pin, OUTPUT);
pinMode(relaisblue_Pin, OUTPUT);
pinMode(relaisyellow_Pin, OUTPUT);
}
// Program to be run //
///////////////////////
void loop() {
// Allocate the state of the input pin of the RED button its associate State variable
buttonred_State = digitalRead(buttonred_Pin);
buttongreen_State = digitalRead(buttongreen_Pin);
buttonblue_State = digitalRead(buttonblue_Pin);
buttonyellow_State = digitalRead(buttonyellow_Pin);
// Red
if (buttonred_State == HIGH) {
digitalWrite(relaisred_Pin, HIGH);
} else {
digitalWrite(relaisred_Pin, LOW);
}
// Green
if (buttongreen_State == HIGH) {
digitalWrite(relaisgreen_Pin, HIGH);
} else {
digitalWrite(relaisgreen_Pin, LOW);
}
// Blue
if (buttonblue_State == HIGH) {
digitalWrite(relaisblue_Pin, HIGH);
} else {
digitalWrite(relaisblue_Pin, LOW);
}
// Yellow
if (buttonyellow_State == HIGH) {
digitalWrite(relaisyellow_Pin, HIGH);
} else {
digitalWrite(relaisyellow_Pin, LOW);
}
}
pins 0 and 1 are used for serial to your PC, so the simulator may be trying to indicate to you that you're performing conflicting actions.
Try two pins other than 0 and 1.
Why, the lack of these will damage both the LEDs themselves and your Arduino.
Don't try telling me that is for just a test. Physics doesn't know that, it will continue inflicting damage until the LEDs loose their brightness and the Arduino fails prematurely.
Thanks both for your responses. For the complete code, see the first post (second post was an addendum :)).
Hmmm... using inputs different from 0 and 1 indeed solved the unexpected behaviour. I wrongly assumed these pins could be used as normal inputs as well. So even in a real life application, inputs 0 & 1 are never to be used on an arduino UNO board?
Right, I see. It's an intermediate (Tinkercad) step to get to the final circuit, which will power 4 12v bulbs through relais. I wanted to eliminate redundant complexities first to fix the basics and trouble shoot the issue.
Then why are you using pull down resistors on your buttons?
Buttons should be connected between input pin and ground, and they should be wired across the diagonals not as you showed them.
Then enable the internal pull up resistors in the software when you use pinMode() and look for a low when a button is pressed and a high when it is not in your software.
Thanks for the tip, Mike. Appreciate it, and will look into it.
I came to this configuration using the Arduino and tinkercad button tutorials, but I see how your suggestion is more straightforward to implement (although I like the intuitive appeal of HIGH - HIGH, being someone without any electronics background :)).
that last image still shows an important misunderstanding.
4-pin buttons are generally two-adjacent-pins-shorted. Lets imagine two different arrangements of that button in your image in post # 10.
Arrangement A: Imagine that there's a wire from the left upper pin to the right upper pin, and another wire from the left lower pin to the right lower pin. In the normal state, your resistor is connected to 5V, and your pin is floating. Press the button, and you connect 5V to your pin, but when not pressed, the pin is indeterminate, and will give you random 0 and 1 values.
Arrangement B: If you rotated that imagined connection arrangement so that the wire was from left upper to left lower, and from right upper to right lower, the resistor would "pull down" the input pin when not pressed, but when pressed, the 5V would pull up the input to a 1.
So, the usual best wiring practice is always to just wire your pin and resistor to the same pin, and ground to the diagonally opposite pin. That way, it doesn't matter which two pins are connected within the button.
So, with your wiring the way it is, one arrangement works, and the other doesn't, and there's no way of telling us how you actually have it wired, because we can't see your circuit. Even for you, you'd have to try it, and rotate the switch 90 degrees if it didn't work, presuming you knew there could be a problem. We struggle with this issue regularly on the forum, where a new user doesn't understand the circuit difference.
Why is it better to use a button-to-ground circuit? Because of the following:
there is a built-in pullup in the processor that you can enable, saving you a resistor (inconsequential for one input, but much more significant when you want to wire many this way). No need to go digging for yet another resistor, either.
When you button-to-ground with buttons on a panel, or metal chassis, your panel or chassis is almost always grounded. If you wired 5V to the button, there's always a chance of shorting out your 5V to ground with a loose or poorly protected wire. Whereas, if ground is used, there's very little chance of a loose ground wire causing you an issue(other than, of course, 'unusual' functionality).
Yes the "official" Arduino way is often wrong, that is not the way that all professionals do things. This applies to several things, like putting supply decoupling on the wrong pins in the shift register tutorials, to using a data select chip on outputs using an unlatched chip.
There is now a way of bringing these things to the attention of the company, but it is a very bureaucratic process.
One way that a red led will light but green and blue won't is by voltage.
Red led (light emitting Diode) has a forward voltage (the minimum V to get current through the diode) in the range 1.8V +/- a few tenths of a volt while green and blue leds have Vf (forward V) around 3V or higher, the photons emitted have energies matching their color with red as low, green middle an blue high, all relative (UV aka black light is higher still) need V to match before any light emits at all.
Resistor for a led to get some brightness by running current through the led (typical led, 5 mA low to 20 mA continuous "max") according to Ohm's Law where Voltage is VCC - Vf.
Amps = (V - Vf) / resistance.
resistance = (V - Vf) / Amps ... where 20 mA is 0.02 Amps.. desired.
If I feed a red lwith forward V = 2) 12V power, what resistor will limit the current to 10 mA?
resistance Ohms = ( 12 - 2 ) / .01 = 1000. Takes more to learn than to do!