Differenze tra le versioni di "Arduino, ESP and i2c devices"
m (→Testing) |
m |
||
Riga 1: | Riga 1: | ||
− | This project permits to | + | 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. | I have successfully tested this program on an ESP-12 (ESP8266) using the Arduino IDE. | ||
Riga 7: | Riga 7: | ||
The hardware "ingredients" needed for this experiments are: | The hardware "ingredients" needed for this experiments are: | ||
* a ESP8266 (in the example it is a ESP12). | * 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). | * A "console cable" (but any USB to TTL converter is okay). | ||
Riga 29: | Riga 29: | ||
=== Software === | === Software === | ||
− | Here is the source code | + | Here is the source code. |
− | |||
<source lang=C> | <source lang=C> | ||
#include <Wire.h> | #include <Wire.h> | ||
− | |||
− | + | void setup() | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | void | ||
{ | { | ||
− | Wire. | + | Wire.begin(); |
− | |||
− | |||
− | |||
− | |||
− | + | Serial.begin(115200); | |
− | + | Serial.println("\nI2C Scanner"); | |
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | void | + | 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 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</source> | </source> | ||
Riga 343: | Riga 93: | ||
If GP00 is unconnected, the program is restarted at power up. | If GP00 is unconnected, the program is restarted at power up. | ||
− | + | Use a terminal emulator to "talk' with yout ESP. | |
− | + | <pre> | |
+ | screen /dev/ttyUSB0 115200 | ||
+ | </pre> | ||
− | + | You'll get the list of i2c devices (each 5 seconds). | |
− | |||
− | |||
− | |||
− | |||
<pre> | <pre> | ||
− | + | 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 | ||
</pre> | </pre> | ||
− | |||
− | |||
− | |||
− |
Versione attuale delle 10:58, 27 giu 2016
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.
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