I've got the arduino beginner project book and I'm reviewing it in preparation for a project that I'd like to do.
It's a small program that does some digital read/digital write to light some lights when a button is pressed.
The strange (to me) thing is that the circuit behaves differently when connected to the Arduino (5v) PWR/GND versus a 9v PWR/GND
I suspect this is because of the difference between 9v and 5v and so I'd need to switch out the resistors, but I'm so newb at electronics that I don't understand what kind of change to make.
Here's the project code:
int switchState = 0;
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
Serial.begin(115200);
Serial.println("Hello!");
}
void loop() {
// put your main code here, to run repeatedly:
switchState = digitalRead(2);
if(switchState == LOW) {
//switch is not pressed
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
} else {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250);
}
}
What do you mean by 'differently? Describe the behaviour. Describe what should happen and what does. Show us a schematic of your circuit, hand drawn and photographed is fine.
What are you connecting where? 9V should go to Vin or the barrel jack, 5V to the 5V pin.
Neither of your videos will play on my PC. However, in the preview before I try to play them, both your Unos appear to be powered from the USB port, so 5V.
I took videos to try to show the difference in behavior. I'm stymied by the restrictions on what I can put in a post as a new member (only 2 links, no pic uploads). Here's a link to the behavior that represents "working". The diagram and the "broken" behavior are in the post itself.
unfortunate- and yes, it does kind of look that way, but the broken behavior is definitely attached to 9V, I could have moved the wires out of the way better
I still do not know to what the 9V is attached. And what is the source of 9V? The ONLY place to attach 9V is the Vin pin or barrel jack, period. Any other place is likely to cause damage. If not right away eventually.
Based on the limited information, you are powering the Uno from 5V from the USB port. Nothing I have seen suggests you are powering it from 9V, certainly not the photo of the page of the book. We want to know how YOU wired it, not what it says in the book. Some photos posted here please and a schematic you have drawn (well, 2, one for 5V, one for 9V)
both the Arduino 5V and the battery 9V are attached in the same location, the top left of the breadboard. That's the only difference between the two setups.
The digitalRead() values are WRT the Arduino system "ground" point.
You can't reliable read a value if there isn't a common ground.
common grounds are more common than you might think. If you have two Arduinos plugged into the USB port of your computer, they'll share a ground that goes through the USB ports and the computer...