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")));
Related
I'm trying to read and control some buttons and LEDs on a Bang & Olufsen infrared eye. It uses a pcf8574 microcontroller to control 2 LEDs and 4 buttons. First of all I just want to get the LEDs to blink. I have successfully done that with an Arduino Uno.
But I want to use it with an Particle Photon so I can connected to the internet. Here I have the code that worked on the Arduino:
#include <Wire.h>
#define beolink (B0100000)
void setup() {
Wire.begin();
}
void loop() {
Wire.beginTransmission(beolink);
Wire.write(0b11111111);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(beolink);
Wire.write(0b00111111);
Wire.endTransmission();
delay(1000);
}
I have no errors on the Particle Photon. I have also tried switching cables and tried 5v instead of the 3.3v. I have connected the pins on the Particle Photon to the same as on the Arduino [SCL(D1) & SDA(D0)].
Thanks to this guy: https://community.particle.io/u/scruffr/summary
It now works. It was not a problem with the code. Apparently you need 2 pull-up resistors on the I2C pins on a Particle Photon. thank you
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.
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
I am running out of pins. I used the pin that I use in LCD as output for a LED however the led is not turning off its always even my code is like this
void setup() {
lcd.begin(20, 4);
pinMode(7 , OUTPUT);
digitalWrite(7, low);
}
Plug off the LCD for a while to see whether your led blinking program is correct.
Anyway, you can't use the same pin for different functions, it's good chance that they will
disturb each other (say, the led will flash silly when you send something to the LCD),
kick out each other (as we see it in your case).
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.