Skip to content

Instantly share code, notes, and snippets.

@jessedc
Created March 10, 2018 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessedc/8cbfb0055b0cd5fcf91e86ef21e53c12 to your computer and use it in GitHub Desktop.
Save jessedc/8cbfb0055b0cd5fcf91e86ef21e53c12 to your computer and use it in GitHub Desktop.
Current Sensor for Arduino
#include <Filter.h>
#include "U8glib.h"
#define RST 8 // Reset
#define CE 9 // CS
#define DC 10 // A0
#define DIN 11 // MOSI
#define CLK 12 // SCLK
#define CURR 0
#define VOLT 1
ExponentialFilter<long> ADCFilter(25, 0);
U8GLIB_PCD8544 u8g(CLK, DIN, CE, DC, RST);
void setup() {
Serial.begin(9600);
ADCFilter.SetCurrent(analogRead(CURR));
}
int roll5AvgValues[5];
int roll5AvgValue = 0;
int roll5Avg(int value) {
roll5AvgValues[0] = roll5AvgValues[1];
roll5AvgValues[1] = roll5AvgValues[2];
roll5AvgValues[2] = roll5AvgValues[3];
roll5AvgValues[3] = roll5AvgValues[4];
roll5AvgValues[4] = value;
roll5AvgValue = (roll5AvgValues[0] + roll5AvgValues[1] + roll5AvgValues[2] + roll5AvgValues[3] + roll5AvgValues[4]) / 5;
return roll5AvgValue;
}
void loop() {
int rawCurrentValue = analogRead(CURR);
int rawVoltageValue = analogRead(VOLT);
ADCFilter.Filter(rawCurrentValue);
float currentValue = (float) map(ADCFilter.Current() - 512, 0, 1023, 0, 5000) / (float) 185;
float voltageValue = (float) map(rawVoltageValue, 0, 1023, 0, 20000) / (float) 1000; //20v
float powerValue = currentValue * voltageValue;
Serial.print(rawVoltageValue);
Serial.print("\t");
Serial.println(currentValue);
// char buffer[20];
// dtostrf(currentValue, 2, 2, buffer);
u8g.firstPage();
do {
u8g.setFont(u8g_font_7x14);
int x = 0;
int y = u8g.getFontLineSpacing();
u8g.setPrintPos(x, y);
u8g.print(currentValue, 2);
u8g.print(" Amps");
y += u8g.getFontLineSpacing();
u8g.setPrintPos(x, y);
u8g.print(voltageValue, 2);
u8g.print(" Volts");
y += u8g.getFontLineSpacing();
u8g.setPrintPos(x, y);
u8g.print(powerValue, 2);
u8g.print(" Watts");
} while( u8g.nextPage() );
delay(250);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment