I²C Scanner mit LC Display

Hello and welcome to our blog post today.

The I2C bus (English:I-Squared-C) is a serial data bus. This is also known as TWI (Two-Wire-Interface).
It allows communication between a Mickro controller and different modules over only 2 lines, and makes it very easy to connect and program sensors and modules.

Each I2C-enabled device has a manufacturer-specified address. For some modules, the lowest 3 bits can be set over 3 control pins. In this case, up to eight similar ICs can be operated on an I2C bus.

In order to find out the I2C address of a module, hobbyists like to use a small sketch like i2cdetect, which searches the different addresses of the I2C bus and lists this in the serial monitor.
However, if you have many I2C modules, or often have to test large quantities, an i2C scanner with display is recommended.

The following parts were used for this project:

- an ESP32 NodeMCU module (because it has 2 separate I2C buses)
- the HD44780 1602 Display Bundle with I2C interface
- 2x Breadboard
- 8x Jumper cable m2f
- 2x jumper cable m2m
- Micro USB cable for ESP32

The circuit is wired as in the following table.

 

Table

When we upload our Sketch to the ESP32, the following message appears alternately in the display:

LC Display 1

As soon as we connect an I2C module to our test lines for SDA and SCL/SCK and supply it with power, the found address appears in the display:

LC Display 2
It is recommended to label the test connections and install the whole thing in a housing.

Label

This means that we have created a simple test device with which we can quickly and reliably determine the I2C address of our modules in the future, and without a computer.

We are happy to provide you with the corresponding sketch:

 

/* I2C slave Address Scanner
Kutscher07: Modified for TTGO TQ board with builtin OLED
Source: https://github.com/espressif/arduino-esp32/issues/977
AZ-Delivery: Modified for ESP32 with 1602 LCD
*/
#include <Wire.H>
#include <LiquidCrystal_I2C.H>
#define SDA1 21
#define SCL1 22
#define SDA2 17
#define SCL2 16
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
uint8_t lcdColumns = 16;
uint8_t lcdRows = 2;
Bool Detected=False;
Adjust the address of the display used here.
LiquidCrystal_I2C Lcd(0x3F, lcdColumns, lcdRows);   Void scan2(){   Detected=False;   Lcd.setCursor(0,0);   Lcd.Clear();   Lcd.Print("I2C found at: ");   Lcd.setCursor(0,1);   uint8_t Cnt=0;   for(uint8_t =0;<128;++){     I2Ctwo.beginTransmission();     uint8_t ec=I2Ctwo.endTransmission(True);     If(ec==0){       Detected=True;       If(<16) Lcd.Print("0");       Lcd.Print("0x");       Lcd.Print(,Hex);       Lcd.Print(" ");       Cnt++;     }   }   If (Detected==False){     Lcd.Clear();     Lcd.setCursor(0,0);     Lcd.Print("No I2C devices");     Lcd.setCursor(0,1);     Lcd.Print("found ...");   }
}   Void Setup(){
I2Cone.Begin(SDA1,SCL1,400000); SDA pin 21, SCL pin 22 TTGO TQ
I2Ctwo.Begin(SDA2,SCL2,400000); SDA2 pin 17, SCL2 pin 16    Lcd.Init();   Lcd.Backlight();
}

Void Loop(){   Lcd.setCursor(0, 0);   Lcd.Clear();   Lcd.Print("Scanning I2C Bus...");   Delay(2000);   scan2();   Delay(4000);
}

 


We hope you enjoyed today's project. Perhaps you have ideas and suggestions on how to expand the whole thing? We look forward to your feedback and say goodbye until the next post.

DisplaysEsp-32Projects for beginners

3 comments

ad

ad

/* I2C slave Address Scanner
Kutscher07: Modified for TTGO TQ board with builtin OLED
Quelle: https://github.com/espressif/arduino-esp32/issues/977
AZ-Delivery: Modified for ESP32 with 1602 LCD
*/
#include
#include
#define SDA1 21
#define SCL1 22
#define SDA2 17
#define SCL2 16
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
uint8_t lcdColumns = 16;
uint8_t lcdRows = 2;
bool detected=false;
//Hier die Adresse des verwendeten Displaya anpassen.
LiquidCrystal_I2C lcd(0×3F, lcdColumns, lcdRows);  

void scan2(){
 detected=false;
 lcd.setCursor(0,0);
 lcd.clear();
 lcd.print(“I2C found at:  ”);
 lcd.setCursor(0,1);
 uint8_t cnt=0;
 for(uint8_t i=0;i<128;i++){
   I2Ctwo.beginTransmission(i);
   uint8_t ec=I2Ctwo.endTransmission(true);
   if(ec==0){
     detected=true;
     if(i<16) lcd.print(“0”);
     lcd.print(“0x”);
     lcd.print(i,HEX);
     lcd.print(" “);
     cnt++;
   }
 }
 if (detected==false){
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print(”No I2C devices");
   lcd.setCursor(0,1);
   lcd.print(“found …”);
 }
}  

void setup(){
I2Cone.begin(SDA1,SCL1,400000); // SDA pin 21, SCL pin 22 TTGO TQ
I2Ctwo.begin(SDA2,SCL2,400000); // SDA2 pin 17, SCL2 pin 16
 lcd.init();
 lcd.backlight();
}

void loop(){
 lcd.setCursor(0, 0);
 lcd.clear();
 lcd.print(“Scanning I2C Bus…”);
 delay(2000);
 scan2();
 delay(4000);
}

Armin Walter

Armin Walter

In der Prozedur setup() fehlt die Initialisierung von IIC.

Einfügen Wire.begin(); vor “I2Cone.begin………”

Michael Reichelt

Michael Reichelt

Hallo, beim Compilieren erhalte ich den Fehler
no matching function for call to ‘TwoWire::TwoWire(int)’
Ich habe es dann mit dem oben erwähnten Script i2cdetect versucht, der produziert den gleichen Fehler – was mache ich falsch?

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN