Skip to content

Commit

Permalink
tested with MCU and MCU S2
Browse files Browse the repository at this point in the history
  • Loading branch information
BjoernLuig committed Dec 6, 2024
1 parent aeac2a3 commit ce82d9f
Show file tree
Hide file tree
Showing 11 changed files with 5,927 additions and 76 deletions.
Empty file.
Binary file not shown.
Binary file not shown.
1,397 changes: 1,397 additions & 0 deletions examples/serial-test/build/sensebox.samd.sb/serial-test.ino.hex

Large diffs are not rendered by default.

2,584 changes: 2,584 additions & 0 deletions examples/serial-test/build/sensebox.samd.sb/serial-test.ino.map

Large diffs are not rendered by default.

Binary file not shown.
1,857 changes: 1,857 additions & 0 deletions examples/serial-test/build/sensebox.samd.sb/serial-test.ino.with_bootloader.hex

Large diffs are not rendered by default.

42 changes: 23 additions & 19 deletions examples/serial-test/serial-test.ino
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
#include <SolarCharger.h>
#include <SolarChargerSB041.h>
#include <Wire.h>
// #include <senseBoxIO.h> // only needer fo senseBox MCU

SolarCharger solar;
SolarChargerSB041 charger;

void setup() {
Wire.begin();
Serial.begin(9600);
}

void loop() {
solar.update();
charger.update();

if (solar.isConnected()) {
Serial.print("Solar Panel Voltage: ");
Serial.println(solar.getSolarPanelVoltage());
Serial.print("Battery Voltage: ");
Serial.println(solar.getBatteryVoltage());
Serial.print("Battery Level: ");
Serial.println(solar.getBatteryLevel());
Serial.print("Charging: ");
Serial.println(solar.isCharging() ? "Yes" : "No");
Serial.print("Fast Charging: ");
Serial.println(solar.isFastCharging() ? "Yes" : "No");
Serial.print("Battery Temperature: ");
Serial.println(solar.getBatteryTemperature());
} else {
Serial.println("Solar charger not connected.");
}
Serial.print("Charger Connected: ");
Serial.println(charger.isChargerConnected() ? "true" : "false");
Serial.print("Solar Panel Voltage: ");
Serial.println(charger.getSolarPanelVoltage());
Serial.print("Battery Voltage: ");
Serial.println(charger.getBatteryVoltage());
Serial.print("Charging: ");
Serial.println(charger.isCharging() ? "true" : "false");
Serial.print("Fast Charging: ");
Serial.println(charger.isFastCharging() ? "true" : "false");
Serial.print("Battery Level: ");
Serial.println(charger.getBatteryLevel());
Serial.print("Good Input Voltage: ");
Serial.println(charger.isGoodInputVoltage() ? "true" : "false");
Serial.print("Battery Present: ");
Serial.println(charger.isBatteryPresent() ? "true" : "false");
Serial.print("Battery Temperature: ");
Serial.println(charger.getBatteryTemperature());
Serial.println();

delay(1000);
}
10 changes: 5 additions & 5 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=SolarChargerSB041
version=1.0.0
author=Dein Name
maintainer=Dein Name <[email protected]>
sentence=Eine Arduino-Bibliothek für das Auslesen des Solarladereglers SB-041.
paragraph=Diese Bibliothek ermöglicht das einfache Auslesen von Daten wie Batterie- und Solarpanel-Spannung, Ladezustand und Temperatur.
author=Björn Luig
maintainer=Björn Luig
sentence=Library to read data from the SB041 solar charger designed for the senseBox.
paragraph=This library enables easy reading of data such as battery and solar panel voltage, charge status, and temperature.
category=Sensors
url=https://github.com/dein-repo
url=https://github.com/sensebox/SolarChargerSB041
architectures=*
55 changes: 41 additions & 14 deletions src/SolarChargerSB041.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,53 @@
#include "SolarChargerSB041.h"

#include "Arduino.h"

SolarChargerSB041::SolarChargerSB041(uint8_t address)
: address(address),
connected(false),
chargerConnected(false),
solarPanelVoltage(-1),
batteryVoltage(-1),
batteryLevel(-1),
charging(false),
fastCharging(false),
batteryLevel(-1),
goodInputVoltage(false),
batteryPresent(false),
batteryTemperature(-1) {}

/*
* I2C i/f with following info on address 0x32:
* - Register 0: cell voltage, 20mV/LSB
* - Register 1: input voltage, 100mV/LSB
* - Register 2: status bits: [B,I,L3,L2,L1,L0,F,C]
* B=battery present >2.8V
* I=Input voltage present >4.5V
* L0-L3=battery status LEDs
* F=Fast charge enabled
* C=Charging
* - Register 3: temperature in C, signed 8-bit
* Thresholds: L0: 3.2V, L1: 3.6V, L2: 3.7V, L3: 3.9V
* Switch to slow charge at 8*C
* Switch to fast charge at 12*C
*/
void SolarChargerSB041::update() {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0 || error == 2) {
connected = (error == 0);
if (error == 0) {
chargerConnected = true;
Wire.requestFrom((uint8_t)address, (uint8_t)4);

uint8_t vbat_raw = Wire.read();
batteryVoltage = 0.02 * vbat_raw;
// scale battery voltage
uint8_t batteryVoltageRaw = Wire.read();
batteryVoltage = 0.02 * batteryVoltageRaw;

// scale panel voltage
uint8_t vin_raw = Wire.read();
solarPanelVoltage = 0.1 * vin_raw;

// status flags
uint8_t flags = Wire.read();
charging = flags & 1;
fastCharging = flags & 2;
if (flags & 32)
batteryLevel = 4;
else if (flags & 16)
Expand All @@ -35,30 +58,34 @@ void SolarChargerSB041::update() {
batteryLevel = 1;
else
batteryLevel = 0;
goodInputVoltage = flags & 64;
batteryPresent = flags & 128;

fastCharging = flags & 64;
charging = flags & 128;

// battery temperature
batteryTemperature = (int8_t)(Wire.read());
} else {
connected = false;
fastCharging = false;
charging = false;
chargerConnected = false; // or worse error
batteryVoltage = -1;
solarPanelVoltage = -1;
charging = false;
fastCharging = false;
batteryLevel = -1;
goodInputVoltage = false;
batteryPresent = false;
batteryTemperature = -1;
}
}

bool SolarChargerSB041::isConnected() const { return connected; }
bool SolarChargerSB041::isChargerConnected() const { return chargerConnected; }
float SolarChargerSB041::getSolarPanelVoltage() const {
return solarPanelVoltage;
}
float SolarChargerSB041::getBatteryVoltage() const { return batteryVoltage; }
int SolarChargerSB041::getBatteryLevel() const { return batteryLevel; }
bool SolarChargerSB041::isCharging() const { return charging; }
bool SolarChargerSB041::isFastCharging() const { return fastCharging; }
int SolarChargerSB041::getBatteryLevel() const { return batteryLevel; }
bool SolarChargerSB041::isGoodInputVoltage() const { return goodInputVoltage; }
bool SolarChargerSB041::isBatteryPresent() const { return batteryPresent; }
float SolarChargerSB041::getBatteryTemperature() const {
return batteryTemperature;
}
58 changes: 20 additions & 38 deletions src/SolarChargerSB041.h
Original file line number Diff line number Diff line change
@@ -1,60 +1,42 @@
#ifndef SOLARCHARGER_H
#define SOLARCHARGER_H
/*
SolarChargerSB041.h - Library to read data from the SB041 solar charger
designed for the senseBox. Created by Björn Luig, Dec. 6, 2024. Released into
the public domain.
*/

#include <Wire.h>

class SolarCharger {
public:
SolarCharger(uint8_t address = 0x32);
#ifndef SOLARCHARGER_H
#define SOLARCHARGER_H
#ifndef SOLARCHARGERSB041_H
#define SOLARCHARGERSB041_H

#include <Wire.h>

class SolarCharger {
public:
SolarCharger(uint8_t address = 0x32);

void update();
bool isConnected() const;
float getSolarPanelVoltage() const;
float getBatteryVoltage() const;
int getBatteryLevel() const;
bool isCharging() const;
bool isFastCharging() const;
float getBatteryTemperature() const;

private:
uint8_t address;
bool connected;
float solarPanelVoltage;
float batteryVoltage;
int batteryLevel;
bool charging;
bool fastCharging;
float batteryTemperature;
};
#include "Arduino.h"

#endif // SOLARCHARGER_H
class SolarChargerSB041 {
public:
SolarChargerSB041(uint8_t address = 0x32);

void update();
bool isConnected() const;
bool isChargerConnected() const;
float getSolarPanelVoltage() const;
float getBatteryVoltage() const;
int getBatteryLevel() const;
bool isCharging() const;
bool isFastCharging() const;
int getBatteryLevel() const;
bool isGoodInputVoltage() const;
bool isBatteryPresent() const;
float getBatteryTemperature() const;

private:
uint8_t address;
bool connected;
bool chargerConnected;
float solarPanelVoltage;
float batteryVoltage;
int batteryLevel;
bool charging;
bool fastCharging;
int batteryLevel;
bool goodInputVoltage;
bool batteryPresent;
float batteryTemperature;
};

#endif // SOLARCHARGER_H
#endif // SOLARCHARGERSB041_H

0 comments on commit ce82d9f

Please sign in to comment.