|
| 1 | +/* Heltec Automation I2C scanner example (also it's a basic example how to use I2C1) |
| 2 | + * |
| 3 | + * ESP32 have two I2C (I2C0 and I2C1) bus |
| 4 | + * |
| 5 | + * OLED is connected to I2C0, so if scan with Wire (I2C0), the return address should be 0x3C. |
| 6 | + * |
| 7 | + * If you need scan other device address in I2C1... |
| 8 | + * - Comment all Wire.***() codes; |
| 9 | + * - Uncomment all Wire1.***() codes; |
| 10 | + * |
| 11 | + * I2C scan example and I2C1 |
| 12 | + * |
| 13 | + * HelTec AutoMation, Chengdu, China |
| 14 | + * 成都惠利特自动化科技有限公司 |
| 15 | + * www.heltec.org |
| 16 | + * |
| 17 | + * this project also realess in GitHub: |
| 18 | + * https://github.com/HelTecAutomation/Heltec_ESP32 |
| 19 | + * */ |
| 20 | + |
| 21 | +#include "Arduino.h" |
| 22 | +#include "heltec.h" |
| 23 | + |
| 24 | +void setup() |
| 25 | +{ |
| 26 | + Heltec.begin(true, false, true); |
| 27 | + Wire.begin(SDA_OLED, SCL_OLED); //Scan OLED's I2C address via I2C0 |
| 28 | + //Wire1.begin(SDA, SCL); //If there have other device on I2C1, scan the device address via I2C1 |
| 29 | +} |
| 30 | + |
| 31 | +void loop() |
| 32 | +{ |
| 33 | + byte error, address; |
| 34 | + int nDevices; |
| 35 | + |
| 36 | + Serial.println("Scanning..."); |
| 37 | + |
| 38 | + nDevices = 0; |
| 39 | + for(address = 1; address < 127; address++ ) |
| 40 | + { |
| 41 | + Wire.beginTransmission(address); |
| 42 | + error = Wire.endTransmission(); |
| 43 | + |
| 44 | +// Wire1.beginTransmission(address); |
| 45 | +// error = Wire1.endTransmission(); |
| 46 | + |
| 47 | + if (error == 0) |
| 48 | + { |
| 49 | + Serial.print("I2C device found at address 0x"); |
| 50 | + if (address<16) |
| 51 | + Serial.print("0"); |
| 52 | + Serial.print(address,HEX); |
| 53 | + Serial.println(" !"); |
| 54 | + |
| 55 | + nDevices++; |
| 56 | + } |
| 57 | + else if (error==4) |
| 58 | + { |
| 59 | + Serial.print("Unknown error at address 0x"); |
| 60 | + if (address<16) |
| 61 | + Serial.print("0"); |
| 62 | + Serial.println(address,HEX); |
| 63 | + } |
| 64 | + } |
| 65 | + if (nDevices == 0) |
| 66 | + Serial.println("No I2C devices found\n"); |
| 67 | + else |
| 68 | + Serial.println("done\n"); |
| 69 | + |
| 70 | + delay(5000); |
| 71 | +} |
0 commit comments