Hi,
I’m trying to test a simple regenerative braking system using a 1.5-5V motor controlled by Arduino Uno used to rotate a wheel for several seconds and stop the motor, a which point the Arduino will read the voltage produced by the motor (which is flowing back into the Arduino) because the wheel continues to spin due to its inertia. However, when I try to stop the motor in the program, it simply reduces its speed.
(Btw - I am new to this.)
Background:
I have connected the Arduino to the motor through a transistor, and the hardware layout is as follows: Analog pin A1 is connected to the left side of the transistor (facing away), though there is a resistor between them. Then, the right side of the transistor is connected to a ground pin on the Arduino. The middle pin of the transistor is connected to the motor through one of the motor’s wires (the black one in this case if it matters). Finally, the other wire from the motor (the red one) is connected to the 5V pin on the Arduino.
Here is the code I used for this:
float totalSum = 0;
float voltage;
float totalAvg;
void setup() {
pinMode(A1, OUTPUT);
Serial.begin(9600);
}
void loop() {
pinMode(A1, OUTPUT);
digitalWrite(A1, LOW);
delay(4000);
digitalWrite(A1, HIGH);
delay(3000);
voltage = readVoltage();
Serial.print(String(voltage) + “\n”);
delay(500);
}
float readVoltage() {
pinMode(A1, INPUT);
int sample_count = 0;
int num_samples = 20;
float sum = 0;
while (sample_count < num_samples) {
sum += analogRead(A1);
sample_count++;
delay(10);
}
float avg = sum/20;
float voltage = avg * (5.0 / 1023.0);
return voltage;
pinMode(A1, OUTPUT);
}
When I upload this to the Arduino and it runs, even though I used digitalWrite(A1, HIGH); to stop the motor, the motor simply reduces its speed. This problem occurs only with the readVoltage() call. If I remove it, the motor succesfully stops. What is the solution to this?
Thanks