How to Reverse DC Motor with Arduino Code?

Continuing the discussion from Welcome to our forums! Please introduce yourself :slight_smile::

Jay @JMcDowell do you have the tethered DC motor with ccw var and cw written below the switch? https://shop.littlebits.cc/products/dc-motor-tethered

Yes. I have two DC motors.

1 Like

Good to know you have the tethered DC motors with “var”. Since you didn’t list wireless Bits or the BLE Bit, is it ok if we refer to your project as autonomous car or programmable car instead of remote control car? Along that line of thinking, below is a project to inspire you.

http://littlebits.cc/projects/autocatch by Tuna Tumer

Below are the analogWrite statements Tuna uses to make a tethered DC motor (switched on either CW or CCW) go forward and stop in intervals. When placed inside the void loop, these same lines of code will cause the tethered motor to go forward and backward if the micro-switch is set to “Var”.

analogWrite(9,255); // 9 is the pin. 255 is the highest analogWrite value, which is equivalent to a slide dimmer in the rightmost position
delay(100); // wait 100 milliseconds
analogWrite(9,0); // 9 is the pin, 0 is equivalent to a slide dimmer in the leftmost position
delay(1000); // wait 100 milliseconds

More info on analogWrite:
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

Please feel free to reply to this thread with pics of your circuit and snippets of code as you go along. Other Bitsters will likely chime in with helpful input and maybe even a little debugging. :wink:

Cheers!

Thank you.

Before I saw this post I figured out a “workaround” that would cause both wheels to move in the same direction and reverse when I pushed a button or slid the slide dimmer back and forth. The only problem is that I can not turn off the autonomous car remotely because that causes a change in direction! Here is the relevant code below:

void loop() {
start = digitalRead(startpin);
delay(50);
go = start;

if(start!=last) {
if(start==HIGH) {
digitalWrite(Motor1,HIGH);
digitalWrite(Motor2,LOW);
}
else {
digitalWrite(Motor1,LOW);
digitalWrite(Motor2,HIGH);
}
}
last = go;
}

1 Like

Ah, nice. In var mode, the DC motor stops when it gets a half signal. See what happens when you replace digitalWrite with analogWrite. The half signal value for analogWrite is 127.

analogWrite(9,127);

More Tips & Tricks: DC motor (tethered): http://littlebits.cc//tips-tricks/dc-motor-tethered

Thanks I will give it a try!

How’s it going, Jay @JMcDowell ? Do you have any updates to share? :smile:

Hello again Jude,
Could help me with the specification of DC Motors that I should be choosing for my project?
It is actually a car which detects objects in its path and navigates around it. I know it is a simple project, but I too am a beginner and need a lot of help over here. I am not even sure where should I start.

Hi, @ayeshakhan . Have you seen the littleBits Droid Inventor Kit? You can code it with a smartphone or tablet, and there is a proximity sensor for object avoidance.

https://littlebits.com/droid-inventor-kit-now-with-coding