-1

I am a newcomer to working with Arduinos (and circuits in general) so when I was tasked with making a simple push-button circuit to control an LED's state, and to send a message back using Serial.println() to show the button had been pressed, I noticed that the LED is dimly lit when I press the button to give it power. However, when I do not use the Serial port at all in my code, then it works perfectly as expected.

My code:

const int ledPort = 8; 
const int detPort = 7; 
void setup() {
  pinMode(ledPort, OUTPUT); 
  pinMode(detPort, INPUT); 
  digitalWrite(ledPort, HIGH); 
  Serial.begin(9600); 
}

void loop() {

  if (digitalRead(detPort) == HIGH) {
    digitalWrite(ledPort, LOW);
    Serial.println("9"); 
    delay(500); 
  }
  else {
    digitalWrite(ledPort, HIGH); 

My circuit:

https://i.stack.imgur.com/mYUYf.jpg

My Board: MEGA2560

2
  • 2
    Your circuit makes no sense whatsoever. Can you draw out how it is actually all connected together as a proper schematic rather than photos that make it hard to see what goes where?
    – Majenko
    Jan 15, 2020 at 11:19
  • Please edit your question so that the image can be seen, without the need to follow a link. Jan 23, 2020 at 7:35

2 Answers 2

0

Your circuit is wired improperly.
It will be easiest for you to simply follow this official tutorial that will show you how to use a push button to turn on an LED. Even though you have a mega, it will work fine for you.

You will see a wiring picture there that is simplified: wiring pic

Plus, the correct code is there:

/* Basic Digital Read
 * ------------------ 
 *
 * turns on and off a light emitting diode(LED) connected to digital  
 * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the
 * concept of Active-Low, which consists in connecting buttons using a
 * 1K to 10K pull-up resistor.
 *
 * Created 1 December 2005
 * copyleft 2005 DojoDave <http://www.0j0.org>
 * http://arduino.berlios.de
 *
 */

int ledPin = 13; // choose the pin for the LED
int inPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
}

Schematic Is Quite Important also, as you attempt to look at the picture and build the circuit you discover how difficult it is to see how things are connected. That leads you to understand how important a simple schematic can be.
I've attempted to turn the pic from the arduino site into a schematic and my interpretation may be wrong or the actual circuit that the Arduino site has built may be wrong. If anyone has comments I will take them into account.

Trying to build from a picture of a circuit is terribly difficult and trying to debug your circuit from a picture is also. That's why when you submit a question you should submit a drawn schematic of how you have wired it up.

schematic

simulate this circuit – Schematic created using CircuitLab

3
  • 1
    There‘s no series resistor for the led...
    – Sim Son
    Jan 23, 2020 at 18:56
  • @SimSon Yeah, it's a bit odd that the circuit from the Arduino site doesn't add one. It's because they simply pushed one LED pin into GND and the other into PIN13 on the Arduino header.
    – raddevus
    Jan 23, 2020 at 19:06
  • Oh, I see. They use a green/blue led, so it might be fine in this case. Not your fault...
    – Sim Son
    Jan 23, 2020 at 19:09
0

Your circuit doesn't make sense to me, either :) I can't see where the LED is getting its ground from, in particular.

Try starting with the circuit shown here and report back!

Good luck!

Your Answer

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

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