Arduino, ESP and i2c devices

Da raspibo.

This project permits to list the Id-s of the devices conncted to the I2C bus.

I have successfully tested this program on an ESP-12 (ESP8266) using the Arduino IDE.

Indice

hardware

The hardware "ingredients" needed for this experiments are:

  • a ESP8266 (in the example it is a ESP12).
  • some i2c devices (I use several jeelabs' plugs like I/O expander, Lux, pressure, lcd).
  • A "console cable" (but any USB to TTL converter is okay).

wiring

i2c: SDA is GP04, SCL is GP05. The pins of jeelabs' lcd plug (this cabling works for all i2c based jeelabs' plugs):

  • P -> +5V or disconnected (unused for lcd-plug)
  • D -> GP04 (SDA)
  • G -> GND
  • + -> +3V3
  • A -> GP05 (SCL)
  • I -> NC (unused for lcd-plug, can be connected to another GPIO for other boards providing useful interrupts).

Console cable: using an Adafruit like cable:

  • Black -> GROUND
  • While -> TXD on ESP
  • Green -> RXD on ESP

DO NOT CONNECT THE RED PIN TO YOUR ESP! The red pin is +5V while ESP need +3V3 so either you have a step-down circuit to convert 5V to 3.3V or you'll need another power source of the right voltage for yout ESP.

Software

Here is the source code.

#include <Wire.h>

void setup()
{
  Wire.begin();

  Serial.begin(115200);
  Serial.println("\nI2C Scanner");
}

void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknow error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

Testing

Load the program on you ESP. Remember that GP00 must be connected to ground at power-up to set the ESP in "flashing mode".

Unplug GP00 otherwise at the next power cycle your ESP will be in "flashing mode" again. If GP00 is unconnected, the program is restarted at power up.

Use a terminal emulator to "talk' with yout ESP.

screen /dev/ttyUSB0 115200

You'll get the list of i2c devices (each 5 seconds).

Scanning...
I2C device found at address 0x24  !
done

Scanning...
I2C device found at address 0x24  !
done

Scanning...
I2C device found at address 0x20  !
I2C device found at address 0x24  !
done

Scanning...
I2C device found at address 0x20  !
I2C device found at address 0x24  !
done

Scanning...
I2C device found at address 0x24  !
done

Scanning...
No I2C devices found
Strumenti personali
Namespace

Varianti
Azioni
Navigazione
Strumenti