How to use I2C Scanner with Digispark to find I2C address | Robojax
This tutorial demonstrates how to use an I2C scanner with a Digispark development board to identify the I2C addresses of connected devices. Knowing the I2C address is crucial for communicating with various sensors and modules using the I2C protocol. This skill is essential for a wide range of projects. For example, you can use this technique to:
- Integrate multiple I2C sensors into a single project, such as an environmental monitoring station.
- Troubleshoot I2C communication problems by verifying the address of a malfunctioning device.
- Quickly identify the I2C address of newly acquired sensors or modules.
This method is particularly useful for devices that don't have their I2C address clearly labeled.
Hardware/Components
To follow this tutorial, you will need the following components:
- Digispark development board (either USB or Micro USB version)
- I2C devices (sensors, modules, etc.) whose addresses you want to identify
- Connecting wires
Wiring Guide
The wiring process involves connecting the SDA and SCL lines from your Digispark to the corresponding pins on your I2C devices (in video at 00:53). The Digispark's SDA pin is typically digital pin 0, and the SCL pin is digital pin 1 (in video at 01:05). Ensure that the VCC and GND pins of your I2C devices are correctly connected to the 5V and GND pins of the Digispark respectively (in video at 01:34). The wiring is similar for various Digispark models (in video at 01:44).
%%WIRING%%
Code Explanation
The provided Arduino code uses the Wire library to scan for I2C devices. The core logic is within the loop() function (in video at 02:38). The code iterates through possible I2C addresses (1 to 127) and attempts communication with each address using Wire.beginTransmission(address) and Wire.endTransmission(). If communication is successful (error == 0), the code prints the address to the serial monitor. The configurable part is mainly the serial baud rate, which is set to 9600 in the Serial.begin(9600) line. You can change this value if needed, but ensure it matches the baud rate of your serial monitor.
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600); // This line sets the serial baud rate.
while (!Serial);
Serial.println("\nI2C Scanner");
}
void loop() {
// ... (rest of the code) ...
}
Live Project/Demonstration
The video demonstrates the process of identifying I2C addresses for several devices (in video at 03:46, 04:14, 05:17). The tutorial shows how to upload the code to the Digispark, connect the I2C devices, and observe the addresses printed on the serial monitor (in video at 03:11). The video highlights that the serial output will appear in any active window, such as Notepad or Microsoft Word (in video at 04:47). It emphasizes the importance of connecting the device after the upload process begins, within a 60-second window (in video at 03:27, 04:22, 05:34).
Chapters
- [00:00] Introduction and Project Overview
- [00:24] I2C Address Explanation
- [00:53] Wiring Instructions
- [01:48] Software Setup and Installation
- [02:35] Code Upload Process
- [03:11] First Device Address Discovery
- [04:06] Second Device Address Discovery
- [05:15] Third Device Address Discovery
- [05:58] Conclusion
// --------------------------------------
//http://playground.arduino.cc/Main/I2cScanner
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not known.
// Version 2, June 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26, 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
// Version 6, November 27, 2015.
// Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit addresses might not be seen properly.
// Watch Video explaining I2C address: https://www.youtube.com/watch?v=bqMMIbmYJS0
//
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("
I2C 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("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found
");
else
Serial.println("done
");
delay(5000); // wait 5 seconds for next scan
}
|||您可能需要的东西
-
易趣在eBay上销售的自动化直流12V LED显示数字延迟定时器ebay.us
-
全球速卖通从AliExpress购买Digispark Attiny85s.click.aliexpress.com
资源与参考
-
外部在eBay上销售的自动化直流12V LED显示数字延迟定时器ebay.us
文件📁
没有可用的文件。
