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:
Whilst for the LED it is consistent:
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);
// }
// }
}
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.
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.
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.
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.