/**
* Attempting to measure current with a DIY shunt resistor,
* and voltage amplification via an opamp.
*
* +--------+
* (PCINT5/~RESET/ADC0/dW) PB5 | 1 8 | VCC
* (PCINT3/XTAL1/CLKI/~OC1B/ADC3) PB3 | 2 7 | PB2 (SCK/USCK/SCL/ADC1/T0/INT0/PCINT2)
* (PCINT4/XTAL2/CLKO/OC1B/ADC2) PB4 | 3 6 | PB1 (MISO/DO/AIN1/OC0B/OC1A/PCINT1)
* GND | 4 5 | PB0 (MOSI/DI/SDA/AIN0/OC0A/~OC1A/AREF/PCINT0)
* +--------+
**/
#include <TinyWireM.h>
#include <Tiny4kOLED.h>
float v1;
float v2;
float voltage;
float resistance = 1000;
float current;
float power;
void setup() {
// Start the I2C communications
oled.begin();
// Two fonts: FONT8X16 and FONT6X8
oled.setFont(FONT8X16);
// To clear all the memory, clear both rendering frames:
oled.clear();
oled.switchRenderFrame();
oled.clear();
oled.switchRenderFrame();
}
void loop() {
// Take the measurments
v1 = (analogRead(3) * 5 / 1023);
v2 = (analogRead(4) * 5 / 1023);
voltage = v1 - v2;
current = voltage / resistance;
power = current * voltage;
// Write the measurements
// The SSD1306 refers to 8 rows of pixels as a 'page'
// page 0: Voltage
oled.setCursor(0,0);
oled.print("Voltage: ");
oled.print(voltage);
oled.println(" V");
// page 1: Current
oled.setCursor(0,2);
oled.print("Current: ");
oled.print(current * 1000);
oled.println(" mA");
// page 2: Power
oled.setCursor(0,4);
oled.print("Power: ");
oled.print(power);
oled.println(" W");
oled.on();
delay(500);
}