Controlling motor does not work but works for LED

I am trying to make an RC car, but for some reason my code works with my LED but not my motor. I tried printing to console what was read from Serial and for my motor there is always this:
image
Whilst for the LED it is consistent:
image
This causes my motor to sometimes turned off even without any further input after enabling it, and sometimes when I want to turn it on/off it is unresponsive (the Tx light does not flash)

const char w = "w";// For comparison
void setup() {
  // put your setup code here, to run once:
  pinMode(dc, OUTPUT);
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  Serial.flush();
}


void loop() {  
  // put your main code here, to run repeatedly:
  int serial;
  char pserial;
  if(Serial.available()){
    serial = Serial.read();
    Serial.println(serial, DEC);
    if (serial == 119) { //w inputted
      pserial = serial;
      digitalWrite(dc, HIGH);
    }
    else if(serial == 115){ //s inputted
      pserial = serial;
      digitalWrite(dc, LOW);
    }
    // else {
    //   Serial.println("Faulty");
    // }
    delay(100);
  }
  // else{
  //   if (pserial == 119){
  //     digitalWrite(dc, HIGH);
  //   }
  //   else if(pserial == 115){
  //     digitalWrite(dc, LOW);
  //   }
  // }
}



Edit: I am just wiring everything like this:

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

  • Simple LEDs require a series dropping resistor !

  • Change
    const char w = "w";// For comparison
    TO
    const char w = ‘w’;// For comparison

Are trying to power your motor directly from your Arduino pin? You can't do that. They can not supply enough current.

Yeah that's what I was doing. How am I supposed to wire it?

You need an h-bridge motor driver circuit/module. These can handle the large currents motors need, allow you to control the speed and direction of the motor, and protect the circuit from damaging reverse voltages coming from the motor.

The choice of module depends on your choice of motor. The h-bridge module must be rated for the voltage and current that the motor(s) need. Give details of the motor (voltage, normal current, stall current) and the forum will help you select an h-bridge driver.

Do not buy an h-bridge based on L298 or L293 chips. They are old and inefficient designs and much better modern equivalents are available.

I bought a L298N motor driver ages ago, but I have no idea on the specs of the motor I am trying to use. It has an amazon listing but nothing about normal current nor stall current.

I was just trying to test my code and didn't realise that I needed to wire everything properly first.

Post a link to the Amazon listing for the motor.

User your multimeter to measure the coil resistance of the motor.

I will test with a multimeter later today.

The Amazon page gives a clue:

6V voltage test: 0.116A

This will be with the motor free-running. With a load, like an RC car, the current will be higher. When the motor starts from stationary, or something jams the motor so it cannot turn, the stall current will probably be at least 10x higher.

The coil resistance will give a better estimate of the stall current.

An Arduino Uno pin can provide up to 0.04A before there is damage, and the reverse voltage from a motor can also damage the Uno, so you should never connect a motor directly to an Arduino pin.

1 Like

Yep, adding in the H bridge got it to work.

I have one more question:
If I am using 2 L298N H bridges driving 2 motors that can vary speed, and an ultrasound sensor, will the 13 pins on the Arduino be sufficient?

EDIT: I can't get the speed to vary, I've remove the jumper on the L298N but the motor doesn't run despite the jumper having to be removed due to the need to vary speed.

On Uno, you cannot use pins 0 or 1. But you can use pins A0 to A5 as digital pins.

To get motor speed to vary, you need to use PWM pins. Not all digital pins are capable of PWM.

How can I show my wiring? I was told that I shouldn't just send screenshots.

What CAD or Photo app have you put your circuit on?

Check if CAD has an EXPORT function, usually under the FILE tab.
Export as jpg if possible, then paste to post.

Tom.. :smiley: :+1: :coffee: :australia:

None, I was asking for said apps.

Draw a schematic on paper, then take a picture of that and post it. Or use Fritzing (schematic view, not breadboard view please), EasyEDA, ...

You should not post screenshots that contain only text, like code or serial monitor output. Instead, copy that text and paste it in to your post between code tags. It requires 1000's of times less data that way.

But if you need to show the forum something on the screen that is not simply text, like a diagram or schematic, then post a screen shot.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.