I am isolating the ATMega328P from the Arduino Uno. I have used it for many other projects, so it does work as intended. Also, I have used my ESP8266-01 for other projects and works great (including serial communication).
I am trying to get the ESP8266-01 to make the ATMega328P pin 13 blink an LED. That is all.
When the ESP8266 is hooked to the TX/RX of the arduino board, the program works exactly as intended. I hook it up the exact same way to the ATMega328P and it doesn't work.
I am using baud rate of 9600 for the communication and using a 16MHz crystal for the ATMega328P.
schematic
simulate this circuit – Schematic created using CircuitLab
The controller is getting 3.3v from the supply as well. And pin 13 is supposed to turn on/off depending on the serial value. Below is the code for the ESP8266:
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(9600);
}
void loop() {
delay(10000);
Serial.write("LED ON");
delay(10000);
Serial.write("LED OFF");
}
And below is the code for the ATMega328P:
#include <SoftwareSerial.h>
SoftwareSerial esp8266(2,3);
void setup()
{
Serial.begin(9600);
esp8266.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
if(esp8266.available())
{
String command = "";
while(esp8266.available())
{
command += (char)esp8266.read();
}
if(command == "LED ON") {
digitalWrite(13, HIGH);
}
if(command == "LED OFF") {
digitalWrite(13, LOW);
}
}
delay(500);
}
As stated, they work great separately, but when I try serial communication with them together, it doesn't work.
P.S. I usually set my ESP8266 baud rate to 115200, but the ATMega328P, I believe, needs to run at 9600 because of the 16MHz clock frequency. Not sure about this part, though.
EDIT:
schematic
-
"but the ATMega328P, I believe, needs to run at 9600". Its hardware UART is fully capable of 115200 baud and higher. The 9600 baud limitation applies to a SoftwareSerial port, which is simulated by programmed 'bit-banging' on another pair of digital I/O pins.JRobert– JRobert01/07/2017 18:59:18Commented Jan 7, 2017 at 18:59
1 Answer 1
Generally, try to debug the problem. Evidently either you're not getting data from your esp8266.available()
and/or esp8266.read()
, or it's not the right data. Put traces (Serial.println(...)
) in, print things out, and find out what's going wrong.
Specifically, I suspect the issue here is that you are relying on all the data being available immediately: you check esp8266.available()
which will tell you whether one or more bytes is available. Then you esp8266.read()
until there are no more bytes available. What if there's a 1ms delay between bytes arriving? You'll get,perhaps LED O
, esp8266.available()
will tell you there's no more data (there's not, yet!) and you stop reading.
You could probably get it working by putting a small delay()
in:
while(esp8266.available())
{
command += (char)esp8266.read();
delay(100);
}
That's a solution (probably) but not a good one. A better solution would be to make your commands LED_ON\n
and LED_OFF\n
- and read until you get a \n
.
-
I tried the above and still no luck. I don't think that would be the issue because the code works with the atmega attached to the uno. All I truly did was isolate it from the uno and added the 16MHz crystal for the external clock.Brett Comardelle– Brett Comardelle01/07/2017 17:59:06Commented Jan 7, 2017 at 17:59
-
I don't know then, sorry. What is actually going wrong? Have you put trace in to find out how far it's getting, where it's failing etc?Mark Smith– Mark Smith01/07/2017 18:02:59Commented Jan 7, 2017 at 18:02
-
I put it back into the uno, and now it is not working. I may have messed up the controller. I will try with a new atmega.Brett Comardelle– Brett Comardelle01/07/2017 18:05:37Commented Jan 7, 2017 at 18:05
-
Okay. It works perfectly with the uno. But when I take the controller off and set it up as shown above, it wont work.Brett Comardelle– Brett Comardelle01/07/2017 18:49:46Commented Jan 7, 2017 at 18:49
-
Omg. I figured it out. I forgot to power the other side of the chip. wow.Brett Comardelle– Brett Comardelle01/07/2017 18:57:49Commented Jan 7, 2017 at 18:57
Explore related questions
See similar questions with these tags.