Skip to content

Commit c27223f

Browse files
author
jenkie
committed
Support external voltage and current sensor
configured via config.h
1 parent 4d67051 commit c27223f

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

Arduino_Pedelec_Controller/Arduino_Pedelec_Controller.ino

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,16 @@ void setup()
494494
motorservo.attach(throttle_out);
495495
#endif
496496

497+
#ifdef USE_EXTERNAL_CURRENT_SENSOR
498+
pinMode(external_current_in,INPUT); //configure as input
499+
digitalWrite(external_current_in,LOW); //disable pull-ups
500+
#endif
501+
502+
#ifdef USE_EXTERNAL_VOLTAGE_SENSOR
503+
pinMode(external_voltage_in,INPUT); //configure as input
504+
digitalWrite(external_voltage_in,LOW); //disable pull-ups
505+
#endif
506+
497507
//increase PWM frequency
498508
#if HARDWARE_REV >= 20
499509
int myEraser = 7;
@@ -560,21 +570,31 @@ void loop()
560570
brake_stat = true;
561571
#endif
562572
//voltage, current, power
573+
574+
#ifndef USE_EXTERNAL_VOLTAGE_SENSOR
563575
voltage = analogRead_noISR(voltage_in)*voltage_amplitude+voltage_offset; //check with multimeter, change in config.h if needed!
564-
#if HARDWARE_REV <= 2
565-
// Read in current and auto-calibrate the shift offset:
566-
// There is a constant offset depending on the
567-
// Arduino / resistor value, so we automatically
568-
// shift it to zero on the scale.
569-
int raw_current = analogRead_noISR(current_in);
570-
if (raw_current < lowest_raw_current)
571-
lowest_raw_current = raw_current;
572-
current = (raw_current-lowest_raw_current)*current_amplitude_R11; //check with multimeter, change in config.h if needed!
573-
current = constrain(current,0,30);
574-
#endif
575-
#if HARDWARE_REV >= 3
576-
current = (analogRead_noISR(current_in)-512)*current_amplitude_R13+current_offset; //check with multimeter, change in config.h if needed!
577-
current = constrain(current,-30,30);
576+
#else
577+
voltage = analogRead_noISR(external_voltage_in)*external_voltage_amplitude+external_voltage_offset; //check with multimeter, change in config.h if needed!
578+
#endif
579+
580+
#ifndef USE_EXTERNAL_CURRENT_SENSOR
581+
#if HARDWARE_REV <= 2
582+
// Read in current and auto-calibrate the shift offset:
583+
// There is a constant offset depending on the
584+
// Arduino / resistor value, so we automatically
585+
// shift it to zero on the scale.
586+
int raw_current = analogRead_noISR(current_in);
587+
if (raw_current < lowest_raw_current)
588+
lowest_raw_current = raw_current;
589+
current = (raw_current-lowest_raw_current)*current_amplitude_R11; //check with multimeter, change in config.h if needed!
590+
current = constrain(current,0,30);
591+
#endif
592+
#if HARDWARE_REV >= 3
593+
current = (analogRead_noISR(current_in)-512)*current_amplitude_R13+current_offset; //check with multimeter, change in config.h if needed!
594+
current = constrain(current,-30,30);
595+
#endif
596+
#else //using external current sensor
597+
current = analogRead_noISR(external_current_in)*external_current_amplitude+external_current_offset;
578598
#endif
579599

580600
voltage_display = 0.99*voltage_display + 0.01*voltage; //averaged voltage for display

Arduino_Pedelec_Controller/config.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ const float current_offset = 0.0; // for Rev 1.3 ONLY! set this valu
204204
// for Rev 1.1 - 1.2 the offset is corrected by software!
205205
const float current_amplitude_R11 = 0.0296; // for Rev 1.1 - 1.2 set this value according to your own current-calibration. Default: 0.0296
206206
const float current_amplitude_R13 = 0.0741; // for Rev 1.3 set this value according to your own current-calibration. Default: 0.0741
207+
// #define USE_EXTERNAL_CURRENT_SENSOR
208+
// #define USE_EXTERNAL_VOLTAGE_SENSOR
209+
const float external_voltage_offset = 0.0;
210+
const float external_voltage_amplitude = 0.00488758553; //default of 0.00488758553 gives Voltage = Voltage at Pin
211+
const float external_current_offset = 0.0;
212+
const float external_current_amplitude = 0.00488758553; //default of 0.00488758553 gives Current = Voltage at Pin
213+
const int external_current_in = A6; //For HW Rev. 2.1: use Pin A6
214+
const int external_voltage_in = A7; //For HW Rev. 2.1: use Pin A7
207215

208216
const char msg_welcome[] PROGMEM = "Welcome";
209217
const char msg_shutdown[] PROGMEM = "Live long and prosper.";
@@ -223,4 +231,4 @@ const char msg_gear_shift_high_gear[] PROGMEM = "High gear active";
223231
const char msg_gear_shift_auto_selection[] PROGMEM = "Auto gear shift";
224232
#endif
225233

226-
#endif
234+
#endif

0 commit comments

Comments
 (0)