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!
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);
}
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
Related
Why does this code lead to Arduino Mega2560 constantly resetting?
void setup() {
Serial.begin(9600);
Serial.println("SETUP");
delay(500); //without this line Serial prints "SESESESESE"
analogWrite(10, 100);
analogWrite(11, 50); //reboots after this line
}
void loop() {
Serial.println("LOOP"); //doesn't reach here
}
Arduino serial outputs the following:
SETUP
SETUP
SETUP
SETUP
SETUP
SETUP
SETUP
SETUP
Things to note:
1) Arduino does not reboot if I substitute pins 10 and 11 with some others (8 and 9 for example), but it does also reboot with pins 12 and 13 (I guess pins 10-13 are somehow special).
2) Arduino does not reboot if I use equal values in the calls to analogWrite (100 and 100 for example).
This behavior should not happen, if you set your pins to:
void setup() {
pinMode(10, OUTPUT); // sets the pin as output
pinMode(11, OUTPUT); // sets the pin as output
Such resets are typical of a bad power supply, noise or bad connections, (bad protoboard), or a bad LED pin (e.g.; resistor too small or damaged, or damaged pin, etc).
If your mega has no HW-defect and a sufficient power source or additional HW attached that causes this defect, it should work as expected.
Apparently, the compiler's optimizations are to blame. After adding these two lines at the start of the program it finally works as it should:
void setup() __attribute__((optimize("-O1")));
void loop() __attribute__((optimize("-O1")));
I am new to low-level programming, and attempting to connect a DHT22 sensor onto my Adafruit M0 Lora for temperature readings. So far I only retrieve NaNs.
The connections I have set up are identical with this sketch, aside from using pin 13 as opposed to pin 2 for sensor input/output. I am aware of the sketch being made for a different feather board, although the logic should still remain the same from what I can understand.
I am making use of Adafruit's DHT library
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
// pin connected to DH22 data line
#define DATA_PIN 13
DHT_Unified dht(DATA_PIN, DHT22);
void setup() {
// start the serial connection
Serial.begin(9600);
// wait for serial monitor to open
while(! Serial);
// initialize dht22
dht.begin();
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
// we are connected
Serial.println();
}
void loop() {
sensors_event_t event;
dht.temperature().getEvent(&event);
float celsius = event.temperature;
float fahrenheit = (celsius * 1.8) + 32;
Serial.print("celsius: ");
Serial.print(celsius);
Serial.println("C");
Serial.print("fahrenheit: ");
Serial.print(fahrenheit);
Serial.println("F");
// save fahrenheit (or celsius) to Adafruit IO
dht.humidity().getEvent(&event);
Serial.print("humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");
delay(5000);
}
Would anyone be able to help point of what I am doing incorrectly? I tried at other bauds than 9600, as well as changing the programmable pin. Any help at all would be greatly appreciated.
I don't think it's a code problem. Pin 13 is special. Choose a different pin.
Specifically:
NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up resistor, it will hang at around 1.7V instead of the expected 5V because the onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor.
From Arduino documentation.
With my Arduino Uno I measure the distance using HC-SR04 ultrasonic sensor with no problems at all using the wiring below.
When I attach ethernet shield, my ultrasonic sensor does not measure distance any more, it constantly says 0cm no matter what. I have tried different digital pin pairs such as 5-7, 6-8, 5-9, 3-5, 2-8 but no luck.
I suspect that HC-SR04 is not compatible with my Ethernet shield but I haven't seen such warning anywhere on the net.
There are no components attached to arduino besides ethernet shield and the ultrasonic sensor itself.
There is no SD Card in SD Card slot.
My ethernet shield works fine while running a web server or web client script.
Digital pins of ethernet shield works fine with all other components such as temperature sensor, motion sensor etc.
Here is the ethernet shield I have;
http://www.ezshopfun.com/product_info.php?products_id=169
Here is my actual circuit;
http://s7.postimg.org/vyi2z36qz/20140826_001130.jpg
http://s7.postimg.org/6eb7ewvzf/20140826_001150.jpg
http://s7.postimg.org/6psnrocff/20140826_001156.jpg
http://s7.postimg.org/y6ro2ooh7/20140826_001229.jpg
http://s7.postimg.org/71a44fsvf/20140826_001247.jpg
Here is my code;
#define trigPin 6
#define echoPin 7
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH) / 2;
distance = duration / 29.1;
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Today I bought a multimeter and tested my circuit. Here are the results;
When my circuit directly attached to Arduino itself;
4.80V & 5.7mA
When my circuit attached to ethernet shield;
3.06V & 3.8mA
I think the problem is that 3.06V is not enough for my HC-SR04 to operate.
Yeah based on this photo
you're not grounding your sensor. You have two power supplies going into it. This, needless to say, is bad for a number of reasons. First and foremost because it wont work ungrounded lol
As others have said, it looks like the main issue is that you need this connected to 5V and check your wiring generally.
However, there is another potential issue:
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
You only need to set the trigger pin high for 10 microseconds, not 1000 microseconds. I don't know if this is an issue or not but there is no need to wait this long. You could potentially be missing some or all of the incoming pulse by waiting that long.
You might want to checkout out some HC-SR04 tutorials too, like this one:
http://superawesomerobots.com/tutorials/hc-sr04-tutorial-for-arduino/
Hope that helps.
I had a similar problem with a wifi shield + ultrasonic sensor. I found switching from pins 13(trig) and 11(echo) to 8(trig) and 3(echo) fixed it.
See here: http://forum.arduino.cc/index.php?topic=201827.0
So I currently have a pic32 arduino. I'm pretty new to this stuff, so any tips would be appreciated.
I have a sensor that has 3 pins, 5VDC, ground, and sensor output. I connected the sensor output and ground header to the two pin slots at PORT0.
For some reason, the program always reads that the sensor is HIGH, even if the sensor is not connected.
If I connect the output to a breadboard with an LED, I can see the LED toggle on and off.
Here is my code:
const int sensor = 0; //sensor port
int sensorState = LOW;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(piezo, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
}
void loop(){
sensorState = digitalRead(sensor);
if(sensorState == HIGH)
alarm();
digitalWrite(ledPin, sensorState);
Serial.println(sensorState);
}
You may have the internal pull-up resistor enabled, so when nothing is connected, it will read high.
Also, these two statements are contradictory:
For some reason, the program always reads that the sensor is HIGH, even if the sensor is not connected.
If I connect the output to a breadboard with an LED, I can see the LED toggle on and off.
So the program always reads high but the LED toggles on or off? Which one is it?
If you manually pull the pin to ground, does your program react the way it is supposed to? If it does, then you should take a look at your sensor circuit.
Your sensor circuit sounds weird - you say
I have a sensor that has 3 pins, 5VDC, ground, and sensor output. I connected the sensor output and ground header to the two pin slots at PORT0
So the sensor output and ground are connected to pin zero? 5v should go to 5v, ground should go to ground, the sensor output should go to pin zero.
Hardware:
Arduino MEGA 2560
2 x MaxBotix MaxSonar-EZ0
Software (relating to Ultrasonics, by no means the entire program):
void setup() {
Serial.begin(9600);
//Ultrasonic Left
pinMode(26, OUTPUT);
pinMode(2, INPUT);
digitalWrite(26, LOW);
//Ultrasonic Right
pinMode(27, OUTPUT);
pinMode(3, INPUT);
digitalWrite(27, LOW);
}
void readSonar() {
digitalWrite(26, HIGH);
delayMicroseconds(25);
digitalWrite(26, LOW);
data[0] = pulseIn(2, HIGH);
digitalWrite(27, HIGH);
delayMicroseconds(25);
digitalWrite(27, LOW);
data[1] = pulseIn(3, HIGH);
return data;
}
Problem:
When the Arduino is first booted, the readings from the two Ultrasonic sensors are not being updated. They are reporting as non-zero values, typically in the range of 500 - 1500. They fluctuate a little (most likely due to noise in the power supply), but tend to stay around the value that they initialise to.
As per the data sheet for these sensors, there are no obstacles within 14 inches of the sensors during the initialisation stage.
By simply disconnecting and reconnecting the cable going to the sensors (from the back of the sensor, not directly to the Arduino inputs), I am able to receive accurate readings from the sensors immediately.
Has anyone had this problem before? My setup() function looks 'normal' from the examples that I have seen. In order to fix this problem, I have connected a switch for the active lines of both sensors. This way the Arduino can boot and then I can give the sensors power. This seems like a botched workaround to me, and I would like a hard-coded software solution, if anyone is able to provide one!
If you look at the characteristics of the sensor it looks clear that you need to start them with the Rx at 0. Look at this link. This is most probably the reason why you have to disconnect the sensor after you start the Arduino to get it working. You also need to make sure that you have pullup resistors connected to avoid unreliability of the readings.