Guest User

Arduino sketch

a guest
Apr 23rd, 2026
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.82 KB | Source Code | 0 0
  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 I2C0
  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. #if defined( WIRELESS_STICK_LITE )
  25.     #include <Wire.h>
  26.     #include "oled/SSD1306Wire.h"
  27.  
  28.     static const uint8_t SCL_OLED = 15;
  29.     static const uint8_t SDA_OLED = 4;
  30. #endif
  31.  
  32. void setup()
  33. {
  34.     Heltec.begin(true, false, true);
  35.     Wire.begin(SDA_OLED, SCL_OLED); //Scan OLED's I2C address via I2C0
  36.     //Wire1.begin(SDA, SCL);        //If there have other device on I2C1, scan the device address via I2C1
  37. }
  38.  
  39. void loop()
  40. {
  41.     byte error, address;
  42.     int nDevices;
  43.  
  44.     Serial.println("Scanning...");
  45.  
  46.     nDevices = 0;
  47.     for(address = 1; address < 127; address++ )
  48.     {
  49.         Wire.beginTransmission(address);
  50.         error = Wire.endTransmission();
  51.  
  52. //      Wire1.beginTransmission(address);
  53. //      error = Wire1.endTransmission();
  54.  
  55.         if (error == 0)
  56.         {
  57.             Serial.print("I2C device found at address 0x");
  58.             if (address<16)
  59.             Serial.print("0");
  60.             Serial.print(address,HEX);
  61.             Serial.println("  !");
  62.  
  63.             nDevices++;
  64.         }
  65.         else if (error==4)
  66.         {
  67.             Serial.print("Unknown error at address 0x");
  68.             if (address<16)
  69.                 Serial.print("0");
  70.             Serial.println(address,HEX);
  71.         }
  72.     }
  73.     if (nDevices == 0)
  74.     Serial.println("No I2C devices found\n");
  75.     else
  76.     Serial.println("done\n");
  77.  
  78.     delay(5000);
  79. }
  80.  
Tags: Arduino
Advertisement
Add Comment
Please, Sign In to add comment