6

I am using an Arduino Uno and Windows 7. My goal is to have an LED light that blinks, and when it blinks, it prints out "Blink" to the serial monitor.

When I run the code below, I am able to print out "Blink" to the serial monitor every 2 seconds, however, the light remains constantly on. When I remove the line

Serial.begin(9600);

the lights will blink, but nothing will print. The code I am running is as follows:

int LED3 = 0;
void setup() {
  // When I comment out the line below, the lights flash as intended, but 
  // nothing prints.  When I have the line below not commented out, 
  // printing works, but the lights are always on (ie do not blink). 

  Serial.begin(9600);  // This is the line in question.
  pinMode (LED3, OUTPUT);
}

void loop() {
  Serial.println("Blink");
  digitalWrite (LED3, HIGH);
  delay(1000);
  digitalWrite (LED3, LOW);
  delay(1000);
}

I am unclear on what causes this behavior and would appreciate an explanation of why this occurs and how to avoid this problem. Thank you!

Marjoram
  • 413
  • 1
  • 7
  • 15
  • @mathews-sunny, what is the plan? if you want to award Erik's answer, then do it. what are you waiting for? – Juraj Mar 09 '21 at 10:52
  • @Juraj My plan is to update the question with more answers and may be updated ones. I don't see any bad in it. The question was answered 2 years back. I just wished to see updates on that. How can you see another plan in it? I want new answers and Will reward it for the best one. Eriks answer is not so clear, So I am waiting. – Mathews Sunny Mar 09 '21 at 11:03
  • @MathewsSunny, it states "Mathews Sunny wants to reward an existing answer. " – Juraj Mar 09 '21 at 13:12
  • @MathewsSunny, so it didn't go as planed – Juraj Mar 15 '21 at 18:54

2 Answers2

16

what causes this behavior ?

Pins 0 and 1 are used for serial communications. It's really not possible to use pins 0 and 1 for external circuitry and still be able to utilize serial communications or to upload new sketches to the board.

From the Arduino Serial reference documentation:

Serial is used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use them in functions in your sketch, you cannot also use pins 0 and 1 for digital input or output.

Just think it this way how can a pin act serial and digital simultaneously ? Yeah that's what you are trying to do !! . You set pin to serial at a baud rate and then you used it for making LED blink.

So, when you do serial.begin(9600); it Sets the data rate in bits per second (baud) for serial data transmission to 9600.Thus you used serial pins in this function, after that you cannot use pins 0 and 1 for digital input or output ( like an LED ). when you comment serial.begin(9600); your pins are free to use and thus you obtain the output.

how to avoid this problem ?

Change the LED from pin 0 to digital pins.

The following code will obtain the result that you expect (I used pin 7 in it) :

int LED3 = 7; //I have changed pin to 7 ( you can use any except 0 and 1 )
void setup() {
  // When I comment out the line below, the lights flash as intended, but 
  // nothing prints.  When I have the line below not commented out, 
  // printing works, but the lights are always on (ie do not blink). 

  Serial.begin(9600);  // This is the line in question.
  pinMode (LED3, OUTPUT);
}

void loop() {
  Serial.println("Blink");
  digitalWrite (LED3, HIGH);
  delay(1000);
  digitalWrite (LED3, LOW);
  delay(1000);
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mathews Sunny
  • 1,796
  • 7
  • 21
  • 31
3

The Arduino is using pins 0 and 1 for serial communications. Pin 0 is RX and 1 is TX, so when you are trying to flash the LED also connected to pin 0, the two start stomping all over each other.

Try to move the LED to a different pin and update your sketch to match, and it should start working.

This page contains information on the Arduino serial connection: https://www.arduino.cc/en/Reference/Serial

Happy hacking

Erik HB
  • 61
  • 4
  • Well, Serial takes over the TX and RX pins, so it doesn't matter what direction or level are you trying to set. (But I'm not sure about input pull-up for the RX) – KIIV May 10 '17 at 10:00