0

The circuit diagram attached shows that the switch is open. Yet, the LED connected to pin 3 of the arduino board lights up.

Arudino spaceship interface circuit diagram

I don't understand this - the circuit is open, so why should pin 3 light up?

I discussed this with one of my friends - he said that each pin on the board receives voltage after the battery is connected. If this is true, then that would answer my question.

This is my code:

int switchState = 0;
//configures the digital pins
void setup() {
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(2, INPUT);
}

//Checks the voltageo of the digital input and chooses the pin for voltage ( pin 2 )
void loop() {
  switchState = digitalRead(2);

  if (switchState == LOW) {
    //Button is not pressed

    digitalWrite(3, HIGH); //Green LED
    digitalWrite(4, LOW); //Red LED
    digitalWrite(5, LOW); //red LED
  }

  else { //the button is pressed
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);

    delay(250); //wait for a quarter second
    //toggle the LEDs
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    delay(250); //wait for a quarter second
  }
} //go back to the beginning of the loop
12
  • Impossible to say without seeing your code. Why does anything happen in code that you can't see? Please edit your question and include it.
    – Nick Gammon
    Commented Apr 8, 2016 at 3:35
  • What I can tell you from the diagram is that pin 2 is grounded when the switch is open and at 5V when it's closed. That wouldn't mean anything for pin 3 unless the chip alters 3 depending on 2. Commented Apr 8, 2016 at 4:18
  • 1
    he said that each pin on the board receives voltage after the battery is connected - not unless the code tells it to.
    – Nick Gammon
    Commented Apr 8, 2016 at 4:35
  • 1
    To elaborate on Nick Gammon's comment, at power on, all the Arduino pins, except power lines, are tri-stated, i.e. in a high-impedance state. This means they will not source nor sink any significant current. Then the Arduino starts executing whatever program was last uploaded into it. If an LED lights up, it does so under the program's command. Commented Apr 8, 2016 at 8:04
  • @NickGammon: Here's the code:
    – user755939
    Commented Apr 9, 2016 at 4:00

2 Answers 2

1

So everyone else knows where this is from enter image description here.


EDIT

I have edited the image you gave to attempt to answer your question on 'wiring' :

wiring

current

The first image shows simply and crudely how the parts are basically connected or there relationship.

The second one shows how the current flows on the circuit, there is a key.

What you need to do is take the information from the link you give in the comment and apply logic to it, like if you used a logic probe and stuck it one end of the switch, the right side in that image, it would be high, if though you do so on the left it would be low. Hence no current flows in that circuit but there is current flow in the circuit looking at it.


What you have not examined is the code related to your project or the description of the project.

Looking at the code:

// Create a global variable to hold the
// state of the switch. This variable is persistent
// throughout the program. Whenever you refer to
// switchState, you’re talking about the number it holds
int switchstate = 0;

void setup() {
  // declare the LED pins as outputs
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // declare the switch pin as an input
  pinMode(2, INPUT);
}

void loop() {

  // read the value of the switch
  // digitalRead() checks to see if there is voltage
  // on the pin or not
  switchstate = digitalRead(2);

  // if the button is not pressed
  // turn on the green LED and off the red LEDs
  if (switchstate == LOW) {
    digitalWrite(3, HIGH); // turn the green LED on pin 3 on
    digitalWrite(4, LOW);  // turn the red LED on pin 4 off
    digitalWrite(5, LOW);  // turn the red LED on pin 5 off
  }
  // this else is part of the above if() statement.
  // if the switch is not LOW (the button is pressed)
  // turn off the green LED and blink alternatively the red LEDs
  else {
    digitalWrite(3, LOW);  // turn the green LED on pin 3 off
    digitalWrite(4, LOW);  // turn the red LED on pin 4 off
    digitalWrite(5, HIGH); // turn the red LED on pin 5 on
    // wait for a quarter second before changing the light
    delay(250);
    digitalWrite(4, HIGH); // turn the red LED on pin 4 on
    digitalWrite(5, LOW);  // turn the red LED on pin 5 off
    // wait for a quarter second before changing the light
    delay(250);
  }
}

From this we see that the default state of this code in the first if() statement is to turn on PIN3 and leave the rest off when the button is not pressed.

From Arduino:

A green LED will be ON, until you press a button. When the Arduino gets a signal from the button, the green light will turn off and 2 other lights will start blinking.

5
  • Thanks @RSM - I understand the code well. But my question is more to do with the circuit diagram (see c03.apogee.net/contentplayer/…) - based on this link, no current flows through the open circuit and the bulb doesn't light up. The code tells Uno "if the switch isn't connected", light up green led that is connected to pin 3. Does that mean that there is some internal wiring between ground and pin 3 that supplies current to the green LED?
    – user755939
    Commented Apr 9, 2016 at 4:13
  • @user755939 Yes, that internal wiring is called the chip. You don't see it in the diagram. The code is controlling the behavior of that internal wiring but if the chip wasn't getting pulled high on the 5V pin and grounded on the GND pin it wouldn't be able to do a blessed thing with any of the other pins no matter what your code says. Commented Apr 9, 2016 at 4:32
  • @user755939 Is your problem that the diagram doesn't show any power source? That's supposed to be assumed. It doesn't come from the chip. It's simply not shown as anything other than 5V and GND. Commented Apr 9, 2016 at 4:38
  • @user755939 I am going to be honest, I don't think you have grasped the basics of electronics and to your credit you are attempting to apply general electrical knowledge to embedded design and electronics. What you need to do is understand the basic construction of a microcontroller, this will help you understand the interaction between it and the code and external components.
    – RSM
    Commented Apr 9, 2016 at 10:54
  • @user755939 please examine the updated answer attempting to simplify the basic of the circuit
    – RSM
    Commented Apr 9, 2016 at 16:17
0

Where is all this confusion coming from?

  if (switchState == LOW) {
    //Button is not pressed

    digitalWrite(3, HIGH); //Green LED

Your code says, if the switch is not pressed, light LED on pin 3.

I don't understand this - the circuit is open, so why should pin 3 light up?

Because that is what the code tells it to do.

Does that mean that there is some internal wiring between ground and pin 3 that supplies current to the green LED?

Get the idea out of your head that this is some wiring issue. Your device is doing exactly what you are telling it to do.

When you digitalWrite HIGH to some pin, the processor activates the output driver MOSFET that attempts to drive the pin to +5V. Therefore current flows through the resistor to the LED which lights up.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.