Components and supplies
Arduino UNO
LM2596 Power Supply Module DC / DC BUCK 3A adjustable
5V Relay - 2 channel optocoupler
6 Position Terminal Strip
MCP-3008 8 channel 10-bit Adc
Resistor 1k ohm
Arduino Proto Shield
Ultrasonic Sensor - HC-SR04 (Generic)
Tools and machines
Soldering iron (generic)
Project description
Code
Battery Multiplexer Arduino Code
arduino
I used an Arduino Uno.
1#include <Wire.h> 2#include <LiquidCrystal_I2C.h> // Using version 1.2.1 3#include <timer.h> 4#include <HCSR04.h> 5 6auto timer = timer_create_default(); // create a timer with default settings 7 8// The LCD constructor - address shown is 0x27 - may or may not be correct for yours 9// Also based on YWRobot LCM1602 IIC V1 10LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 11 12int zcnt = 0; 13int acnt = 0; // general purpuse right now 14int chargeTime = (10 * 1000); // how much time to spend on each battery 15int charge_time_counter = 59; // how many chargetimes were passed? 16int charge_time_max = 60; // how many chargetimes were passed? 17int relay1 = 3; // each relay on it's own pin 18int relay2 = 4; 19int relay3 = 5; 20int relay4 = 6; 21int current_relay = 0; // which relay is the one on now? 22int max_relays = 3; // how may relays - 0 based. Start expecting all three. 23float volts1; // what's the voltage of each battery 24float volts2; 25float volts3; 26int min_volts = 1; // what is the minimum voltage a batter voltage should be to be charged. 27int volts_update_count = 5; // how often to update the voltage reading in seconds 28int volts_update_counter = 0; // track the count of seconds gone by 29int volts_update = (500); 30int read_distance_update = (500); // update the distance reader every millis 31int time_on_counter = 0; // counter for the display on/off 32int time_on_count = 100; //seconds to be on 33int heart_beat = 0; // flip this 0 to 1 to 0 to show the heart beat 34 35UltraSonicDistanceSensor distanceSensor(11, 12); // Initialize sensor that uses digital pins 13 and 12. 36int distance; 37 38int relays[] = { relay1, relay2, relay3 }; 39int is_live[] = { 0, 0, 0}; // 1 if live, 0 if not. I have only 3 40 41int debug = 5; // set this to the level of debug msgs to go to the serial printer 42 43void all_change_to(int which) { // change all relays to HIGH or LOW, you tell me which:) 44 digitalWrite(relay1, which); 45 digitalWrite(relay2, which); 46 digitalWrite(relay3, which); 47} 48 49void toggle_relays(int whichOne) { 50 lcd.setCursor(0, 0); // show which relay is the current one on 51 lcd.print("Relay on:"); 52 lcd.print(current_relay + 1); // current_relay is zero based 53 all_change_to(HIGH); // turn all off first 54 digitalWrite(relays[current_relay], LOW); // and set the one that should be on on 55 acnt++; // just a counter to show me it's working 56 lcd.print(" "); 57 lcd.print(acnt); 58} 59 60void checkRelays() { 61 // move the current_relay to the next relay 62 // if we hit the max, start at 0 63 // while you're here, call toggle_relays to make sure we're not charge a blank or dead one 64 charge_time_counter++; 65 if (charge_time_counter >= charge_time_max) { 66 current_relay++; 67 if (current_relay >= max_relays) { 68 current_relay = 0; 69 } 70 toggle_relays(current_relay); 71 charge_time_counter = 0; 72 } 73} 74 75void read_distance() { 76 // reaad the distance of anything from the HC-S204 77 // if it's something within 80cm then turn the display on and reset the display on counter 78 // if the counter is reached then nothing is within 80cm so turn the display off 79 distance = distanceSensor.measureDistanceCm(); 80 // uncomment these to see the actual distance measured 81 // Serial.print("Distance in CM: "); 82 // Serial.println(distance); 83 if (distance < 80) { // can be anything under 200 for my system uncomment above and check yours 84 lcd.backlight(); // turn display on 85 time_on_counter = 0; 86 } else { // maybe going off? 87 time_on_counter++; 88 if (time_on_counter > time_on_count) { 89 time_on_counter = 0; 90 lcd.noBacklight(); 91 } 92 } 93} 94 95void read_show_volts() { 96 // read the volts at each battery input 97 // then if less then min_volts, there is nothing there worth charging, skip that connection 98 // this gets called over and over so if a wire gets knocked off or 99 // whatever it won't be in the charge loop 100 volts1 = analogRead(0); 101 volts1 = (volts1 * 0.016); 102 lcd.setCursor(0, 1); lcd.print(" "); // clear the line 103 lcd.setCursor(11, 0); 104 lcd.print(volts1); 105 volts2 = analogRead(1); 106 volts2 = (volts2 * 0.0164); 107 lcd.setCursor(4, 1); 108 lcd.print(volts2); 109 volts3 = analogRead(2); 110 volts3 = (volts3 * 0.0166); 111 lcd.setCursor(11, 1); 112 lcd.print(volts3); 113 // now test the voltages. If less than 10, then let's assume dead/bad or no battery 114 // so take it out of rotation 115 // start by clearing all relays out 116 int temp_cnt = 0; 117 // set all arrays to 0 - that's off 118 relays[0] = 0; 119 relays[1] = 0; 120 relays[2] = 0; 121 if (volts1 > min_volts) { 122 relays[temp_cnt] = relay1; // relay 1 is good 123 temp_cnt++; 124 } 125 if (volts2 > min_volts) { 126 relays[temp_cnt] = relay2; // relay 2 is good 127 temp_cnt++; 128 } 129 if (volts3 > min_volts) { 130 relays[temp_cnt] = relay3; // relay 3 is good 131 temp_cnt++; 132 } 133 max_relays = temp_cnt; 134 // this is a heart bet on the lcd - just shows me it's running 135 lcd.setCursor(0, 1); 136 if (heart_beat == 1) { 137 lcd.print("<>"); 138 heart_beat = 0; 139 } else { 140 lcd.print("><"); 141 heart_beat = 1; 142 } 143 lcd.print(charge_time_counter); 144 read_distance(); 145} 146 147void setup() 148{ 149 Serial.begin(19200); 150 Serial.println("Starting"); 151 lcd.begin(16, 2); // sixteen characters across - 2 lines 152 lcd.backlight(); 153 pinMode(relay1, OUTPUT); 154 pinMode(relay2, OUTPUT); 155 pinMode(relay3, OUTPUT); 156 all_change_to(HIGH); 157 // setup timers. 3 timers - the time to turn the charger on each battery, 158 // how often to show the voltage update 159 // and how often to check for distance read to turn on the display 160 read_show_volts(); // do the first time so we don't have to wait for the timer. 161 checkRelays(); 162 timer.every(chargeTime, checkRelays); 163 timer.every(volts_update, read_show_volts); 164} 165 166void loop() 167{ 168 timer.tick(); // tick the timer 169} 170 171
Downloadable files
Battery Multiplexer Schematic
Schematic for multiplexer
Battery Multiplexer Schematic
Battery Multiplexer Schematic
Schematic for multiplexer
Battery Multiplexer Schematic
Comments
Only logged in users can leave comments
leonzak
0 Followers
•0 Projects
Table of contents
Intro
13
0