This Arduino sketch serves as a versatile controller, performing two primary functions:
- I2C Peripheral Emulation: It emulates a power supply unit, responding to I2C commands as a slave device.
- Current Sensing: It reads analog data from current sensors, converts it to Amperage, and outputs the values to the serial monitor.
- I2C Slave Emulation: Emulates an I2C device at address
0x58. - Command Processing: Responds to a variety of I2C commands to simulate a power supply's status and readings.
- Analog Current Measurement: Reads from three analog pins (A0, A1, A2) to monitor current.
- Serial Output: Prints the measured current values to the serial port for debugging and monitoring.
- An Arduino-compatible board (e.g., Arduino Uno, Nano).
- I2C bus connection for communication with a master device.
- Up to three analog current sensors connected to pins
A0,A1, andA2.
This is the main file for the sketch.
-
setup():- Initializes serial communication at 9600 baud.
- Initializes the I2C bus with slave address
0x58. - Registers the
requestEventandreceiveEventhandlers for I2C communication.
-
loop():- Continuously reads analog values from pins
A0,A1,A2. - Converts the raw analog-to-digital converter (ADC) readings into Amperage.
- Prints the current values for each sensor to the serial monitor.
- Includes a 500ms delay to control the reading frequency.
- Continuously reads analog values from pins
-
convert_voltage_in(int reading):- Takes a raw ADC reading as input.
- Converts the reading to a voltage value based on the system's
VOLTAGE(3.3V). - Calculates the current in Amps based on the sensor's characteristics.
-
receiveEvent(int byteCount):- This function is called whenever the I2C master sends data to this slave device.
- It reads the incoming byte, which is treated as a command code for the
requestEventhandler.
-
requestEvent():- This function is called whenever the I2C master requests data from this slave device.
- It uses a
switchstatement to check the last command code received viareceiveEvent. - Based on the command, it sends back predefined data, emulating the behavior of a power supply. This includes responses for operation status, voltage/current readings, manufacturer information, and more.
-
Hardware Setup:
- Connect your Arduino board to your computer.
- Connect the I2C master to the SDA and SCL pins of your Arduino.
- Connect your analog current sensors to pins
A0,A1, andA2.
-
Flashing the Sketch:
- Open the
main.cppfile in the Arduino IDE or your preferred editor. - Select the correct board and port.
- Upload the sketch to your Arduino.
- Open the
-
Monitoring:
- Open the Serial Monitor in the Arduino IDE.
- Set the baud rate to
9600. - You will see the current readings from the connected sensors printed to the monitor.
-
I2C Communication:
- Your I2C master device can now communicate with the Arduino at address
0x58. - To read data, the master should first write a single command byte (e.g.,
0x8Bto read VOUT) and then immediately request data from the slave.
- Your I2C master device can now communicate with the Arduino at address