Using Serial.print and digitalWrite in the same Arduino Script

I am using Arduino Uno and Windows 7. My goal is to blink the LED, and when it blinks, it outputs "Blinking" to the serial monitor.

When I run the code below, I can print "Blink" to the serial monitor every 2 seconds, however the light stays on all the time. When I remove the line

Serial.begin(9600);

      

the light flashes but nothing will be printed. The code I'm running looks like this:

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 don't understand what is causing this behavior and I would like to explain why this is happening and how to avoid this problem. Thank!

+3


source to share


2 answers


what is causing this behavior?

Pins 0 and 1 are used for serial communication. It is not possible to use pins 0 and 1 for an external circuit and still be able to use serial communication or download new sketches to the board.

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

Think if this is how you can operate both serial and digital at the same time? Yes, what are you trying to do !! ... You put a pin on a baud-rate serial port and then use it to flash the LEDs.

So when you do it serial.begin(9600);

, it sets the baud rate for serial data to 9600. So you used serial pins in this function, after which you cannot use pins 0 and 1 for digital input or output (e.g. LED). when you comment serial.begin(9600);

, your contacts are free to use and therefore you get results.



how to avoid this problem?

Change the LED from pin 0 to digital pins.

The following code will get the output 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);
}

      

+8


source


Arduino uses pins 0 and 1 for serial communication. Pin 0 is RX and 1 is TX, so when you try to blink, the LED also connects to pin 0, both start stomping on top of each other.

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



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

Happy hacking

+1


source







All Articles