Search code examples
arduinoatmegaserial-communication

Replicating Arduino's serial communication on atmega328p


I have a simple circuit, with an ESP8266 and an Arduino Nano, communicated via Serial.

To test this communication, I send a simple "TURN ON" string and the Arduino should turn on a LED.

This is the Arduino code:

void setup() {
  Serial.begin(9600);
  pinMode(3, OUTPUT);
}

void loop() {

  if(Serial.available() > 0) 
  {
    String command = "";
    while(Serial.available())
    {
      command += (char)Serial.read();
      delay(1);
      if (command == "T"){
        while(Serial.available())
        {
          command += (char)Serial.read();
          delay(1);
        }
      }else{
        command = "";
      }
    }

    if(command == "TURN ON") {
      digitalWrite(3, HIGH);  
    }

    if(command == "TURN OFF") {
      digitalWrite(3, LOW);
    }
  }

  delay(500);
}

This works fine.

Now, I would like to replace the Arduino with an Atmega328p.

I have the basic circuit: enter image description here

Plus the led, and the TX and RX pins connected to the ESP8266.

If I upload the same code to the Atmega328p, it doesn't work, so I guess I'm missing something very important (like UART configuration or something) but I don't know what that is


Solution

  • In case that someone arrives here with a similar issue:

    Yes, it should work as I intended.

    I was using a damaged breadboard! (you can't receive a message if the pins are not connected ... )