I am trying to control a hoverboard hub motor using an RC receiver, Arduino UNO R4 Minima, and a cheap speed controller.
RC receiver: Radiomaster R88
Speed controller: RioRand 350W 6-60V PWM DC Brushless Electric Motor Speed Controller with Hall
I was able to run the motor using just analog write and values from 0-255 and it was working as intended.
When I try taking the input of the rc receiver, use the map function, and analog write it does this weird stutter. Jumps speeds randomly and just isn't smooth motion for some reason.
Below is my code.
#define CH1In 2 //rc reciever to arduino
#define CH1Out 3 //arduino to speedcontroller
unsigned long duration; //pwm from rc reciever
int escvalue; //pwm value for analogwrite function for speed controller
void setup() {
Serial.begin(115200);
pinMode(CH1In, INPUT);
pinMode(CH1Out, OUTPUT);
}
void loop() {
duration = pulseIn(CH1In, HIGH);
escvalue = map(duration, 1000, 2000, 0, 255);
analogWrite(CH1Out,escvalue);
}
If pulseIn times out, duration will be 0, resulting in setting escvalue to -255, which analogWrite converts to 1 (which is the same result that you would have obtained if duration jumped to a value in the range 1004–1007).
Can you Serial.print() those values, then put those values in a spreadsheet to graph them (INSERT >> CHART >> LINE), which may show a trend and help find the solution?
You can further constrain these values so "0" is never in the motor drive equation, try something very different, like the following... and observe if "jumpy" still happens.
I have checked them. It run fine with the built in potentiometer on the speed controller and just using straight up analogWrite from the Arduino. It's something to do with the rc receiver input/mapping. I tried a different receiver and it's doing the same thing.
map() looks good. pulseIn() is a mystery to me. I like to blame the things I do not understand. : ) (kidding - but it makes me feel better). Trying to find if pulseIn() is the cause...
New discovery. I have been powering the arduino via the Vin pin with a 4S battery. I tried running it while plugged into the computer via USBC to check the values real time, and it was working as intended. I wonder if I was hitting some kind of current limit, or if there is some inefficiencies with vin dropping voltage to 5V. Guess I'm gonna go find a BEC and power it with the same battery but can plug into USBC.
Fully charged is 4.2v per cell, but this one is probably around 3.7 as it was sitting at storage voltage when I started using it. It says that it supports voltage up to 24v on the product page. Might have a 2S battery around that I can try. I tried a 3S and it was doing the same thing. I'm also using the pin cables and header on the board to connect the battery. Wonder if that's a bad way to connect it.
I wonder if the load on the battery is not enough to let the battery management know it is being used?
Ebikes have two levels of auto-shutoff. The first level is "sleep" after a few minutes when the system is left ON with the display and any peripheral actively draining power. Eventually, the BMS will go to a deeper sleep when activity has not occurred for perhaps two weeks.