I want to communicate with a vacuum cleaner battery using i2C.
I think i wrote the right code, but it keeps returning only -1. Can you help me?
#include <Wire.h>
const byte batteryAddress = 0x49;
const byte voltageRegister = 0x09;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
}
void loop() {
// put your main code here, to run repeatedly:
float batteryVoltage = readBatteryVoltage(batteryAddress, voltageRegister);
Serial.print("Battery Voltage: ");
Serial.print(batteryVoltage);
Serial.println(" V");
delay(1000);
}
float readBatteryVoltage(int deviceAddress, byte registerAddress)
{
float voltage = 0.0;
Wire.beginTransmission(deviceAddress);
Wire.write(registerAddress);
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 2);
byte highByte = Wire.read();
byte lowByte = Wire.read();
int voltage = (highByte << 8) | (lowByte >> 8);
return voltage;
}