-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3.4-working-voltage-reads
106 lines (94 loc) · 3.23 KB
/
v3.4-working-voltage-reads
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* solar-charger-arduino-nano-v3
* v3.4-working-voltage-reads
* Todo:
*[x] get tmp36 sensor working with code
*[x] get display working
*[x] display tmp36 on display
*[x] get mosfet driver circuit working
*[x] create voltage divider for solar
*[x] create voltage divider for battery
*[ ] get shunt resistors working on solar and battery
*[ ] create sleep arduino when low solar voltage
*[ ] create sleep arduino when battery voltage is less than 6V
*[ ] create cut off solar panel when battery voltage is 8.5V
*[ ] create cut off solar panel based on temp increase (perhaps +10c?)
*/
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define pwmMax 255
#define sensorPin A0
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int pwmPin = 6;
float batteryMaxV = 8.5;
float solarVoltage = 0.0;
float batteryVoltage = 0.0;
void setup()
{
Serial.begin(9600);
pinMode(pwmPin,OUTPUT);
TCCR1B = TCCR1B & B11111000 | B00000101; // 30Hz Pin 9 & 10
TCCR0B = TCCR0B & 0b11111000 | 0x05 ; // 61Hz Pin 5 & 6
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
// Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
}
display.clearDisplay();
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(10); // Where c is a value from 0 to 255 (sets contrast e.g. brightness)
}
void loop()
{
/*******************Voltage Reads***********************/
int sampleCount = 0;
while (sampleCount < 50)
{
solarVoltage += analogRead(A3);
batteryVoltage += analogRead(A2);
sampleCount++;
delay(5);
}
/*
* read the voltage from the two voltage dividers, for solar and battery voltages
* Divide by 50 samples
* Multiply by 5V
* Divide by 1024 ADC values
* Multiply by division factor R1 / R2 ... R1 = 10k , R2 = 1k
*/
solarVoltage = (((float)solarVoltage / 50) * 4.7) / 1024.0 * 11;
batteryVoltage = (((float)batteryVoltage / 50) * 4.7) / 1024.0 * 11;
/*******************PWM Duty Cycle***********************/
//duty cycle is inversed by mosfet,
//so 90% duty cycle = 10% on.
//10% duty cycle = 90% on.
analogWrite(pwmPin, pwmMax * 0.05); //5% duty cycle or on 95%
/*******************Temperature Reading***********************/
// Get a reading from the temperature sensor:
int reading = analogRead(sensorPin);
// Convert the reading into voltage:
float voltage = reading * (5000 / 1024.0);
// Convert the voltage into the temperature in Celsius:
float temperature = (voltage - 500) / 10;
/*******************Display***********************/
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.print("Temp:");
display.print(temperature);
display.println("C");
display.print("Battery: ");
display.print(batteryVoltage);
display.println("V");
display.println("-------");
display.print("Solar Panel: ");
display.print(solarVoltage);
display.println("V");
display.display();
display.clearDisplay();
delay(50); // wait a second between readings
}