Turai Botond
Published © GPL3+

ESP8266 Arduino Communication

You can upgrade your Arduino with WiFi connectivity.

AdvancedFull instructions provided17,570
ESP8266 Arduino Communication

Things used in this project

Story

Read more

Schematics

ESP-Arduino circuit

Code

Receiver code

Arduino
#define sda 3
#define scl 2

/*

   RECEIVER


   //Tested on Arduino UNO, Arduino MEGA2560, ESP-8266
*/

void setup()
{
  Serial.begin(2000000);
  pinMode(sda,INPUT_PULLUP);
  pinMode(scl,INPUT_PULLUP);
}

void loop()
{
  if(digitalRead(scl)==0)
  {
    byteRead();
  }
}

byte byteRead()
{
  byte _msg=0;
  while(digitalRead(scl)==0){}
  uint32_t to=micros();
  for(int i=0;i<8;i++)
  {
    bool t=digitalRead(scl);
    bitWrite(_msg,i,digitalRead(sda));
    while(t==digitalRead(scl) && (to+300)>micros()){}
    if((to+300)<micros())
    {
      return 0;
    }
    else
    {
      to=micros();
    }
  }
  Serial.println(_msg);
}

Transmitter code

Arduino
#define sda 2 //GPIO2 on ESP-8266
#define scl 0 //GPIO0 on ESP-8266

#define baud 50
bool t = 0;
/*

   TRANSMITTER

   //Tested on Arduino UNO, Arduino MEGA2560, ESP-8266

*/

void setup()
{
  Serial.begin(2000000);
  pinMode(sda, OUTPUT);
  pinMode(scl, OUTPUT);
  digitalWrite(sda, 1);
  digitalWrite(scl, 1);
}

void loop()
{
  transmit(10);
  for(byte k=0;k<20;k++)
  {
    transmit(k);
    delay(300);
  }
  transmit(10);
  delay(5000);
}

void transmit(byte _msg)
{
  digitalWrite(scl, t);
  t = !t;
  delayMicroseconds(baud);
  
  for (int i = 0; i < 8; i++)
  {
    digitalWrite(scl, t);
    t = !t;
    digitalWrite(sda, bitRead(_msg, i));
    delayMicroseconds(baud);
  }
  digitalWrite(scl, 1);
  digitalWrite(sda, 1);
  t=0;
  delay(20);
}

Credits

Turai Botond
5 projects • 21 followers
Check out my youtube channel for exciting projects: www.youtube.com/@botondturai

Comments