1

So I'm going through Getting Started With Arduino. I skipped to making a LED blink when you press a button.

First I wrote the code, then I copied it directly from the .pdf I have, neither has worked.

The code is here:

// Example 02: Turn on LED while the button is pressed
const int LED = 13;   // the pin for the LED
const int BUTTON = 7; // the input pin where the
                      // pushbutton is connected
int val = 0;          // val will be used to store the state
                      // of the input pin
void setup() {
  pinMode(LED, OUTPUT);   // tell Arduino LED is an output
  pinMode(BUTTON, INPUT); // and BUTTON is an input
}
void loop(){
  val = digitalRead(BUTTON); // read input value and store it
  // check whether the input is HIGH (button pressed)
  if (val == HIGH) {
    digitalWrite(LED, HIGH); // turn LED ON
  } else {
    digitalWrite(LED, LOW);
  }
}

enter image description here

  • You need to have a resistor in series with that LED or you will damage it and/or your board. – Nick Gammon Mar 9 '16 at 5:56
  • The sketch uses pin 13, i.e. the built in LED; I think the one at the top right is unconnected. – microtherion Mar 9 '16 at 5:58
  • @Nick: The book doesn't say that. It shows a drawing with the LED connected directly to pin 13 and ground. – Jones Mar 9 '16 at 6:02
  • Chuck the book out and get a different one. That's incorrect. Also, can you get the blink sketch to work? Disregard the button and just see if you can get an LED to blink on its own. – uint128_t Mar 9 '16 at 6:03
  • 1
    It looks like you are right. However that book was written years ago when the Arduino had a built-in resistor. Modern ones don't. – Nick Gammon Mar 9 '16 at 6:10
2

Other than what what Nick Gammon said that you need the resistor from the board to led, as that will cause your microcontroller pins to get damaged.

The other possible problem is the two breadboards you have put together, the power rails have not been bridged between the two boards. This means that the switch is not providing any logical state to the microcontroller.

Also the only reason why the led is show in the book as a separate thing is that it is meant as an educational exercise to make you build something, the L led will do the same thing.

  • the power rails have not been bridged between the two boards - you are totally right. You can see from the red line on the board that the two sides are not connected. – Nick Gammon Mar 9 '16 at 6:18
  • In fact they are just two smaller boards which happen to be adjacent. Those power and ground lines won't be connected. – Nick Gammon Mar 9 '16 at 6:19
  • @NickGammon I think some believed the power is transfered wirelessly when placed adjacent to one another, which unfortunately many newcomers are ill informed about the way the things work. Its the second time I've seen this. – RSM Mar 9 '16 at 6:22
  • Nah, they just don't realize that the board doesn't run all the way through. I've had a few lengthy arguments about this in the past. :) – Nick Gammon Mar 9 '16 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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