2 potentiometers controlling 2 motors via PWM (1 receives no PWM)

Hi there.

As per the title I have 2 potentiometers controlling 2 motors via PWM but 1 of the motors receives no PWM no matter how I twist the potentiometer.

I am no coding expert so I combined code from the website (https://tronixlabs.com.au/news/tutorial-l298n-dual-motor-controller-module-2a-and-arduino/) and from the arduino example called AnalogInput. I then made it so that the potentiometer values are output to the sensor so that I could tune the pots to output 0-255. Potentiometer values tested to range from 0-1023 and the motor PWMs are set to range from 0-255 so I figured that dividing by 4 in the code like this "analogWrite(enA, sensor1Value/4);" for both motors would work, but for some reason multiplying by a bit over 16 makes the motor output 255! Therefore, I have the code as "analogWrite(enA, sensor1Value*16);" and the same for motor B of course. The thing is that motor B only receives a sensor value of zero at all times. The two pots and motors are wired in exactly the same way. Motor A uses pin A0 on the Arduino and motor B uses pin A1. Both negatives on the potentiometer go to GND and both positives go to 5V. Any ideas as to how to get the 2nd pot to give a reading to motor B rather than nothing nada zilch? The code is below. Also, photo attached. Thanks.

int sensor1Pin = A0; // select the input pin for the potentiometer
int sensor1Value = 0; // variable to store the value coming from the sensor
int sensor2Pin = A1; // select the input pin for the potentiometer
int sensor2Value = 0; // variable to store the value coming from the sensor

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;

void setup()
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

Serial.begin(9600);
}

void loop()
{
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, sensor1Value16);
delay(100);
Serial.print(" Sensor 1 = ");Serial.print(sensor1Value
16);

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, sensor2Value16);
delay(100);
Serial.print(" Sensor 2 = ");Serial.print(sensor2Value
16);
}
// read the value from the sensor:
sensor1Value = analogRead(sensor1Pin);
sensor2Value = analogRead(sensor2Pin);
}

analogRead() gives you a value between 0 and 1023 and analogWrite() takes a value between 0 and 255 so why are you multiplying the read value by 16. Dividing it by 4 would work.

...R

Hi Robin. Yes, it is weird. I did say in the post that I would think dividing by 4 would be correct but I found out by experiment that multiplying by 16 gave the right values. I'm not sure if this is part of the problem though. The problem is one of the motors is getting zero PWM signal no matter how I turn the pot attached to it. Thanks for your input.

What happens when you use divide by 4? That is the right solution so work from that.

What happens when you swap motors and/or only have one motor connected, or swap the Arduino to drive the other half of the L298 from the pins for the first half (hope that makes sense)? You need to eliminate the possibility that one of the motors or part of the L298 is faulty.

...R

try using serialprint to make sure your analog inputs are both giving values 0..1023

  • that's a start

regards
Allan.

Really confused :confused: Tried a whole new motor board and same problem. Both motors are okay because I've swapped around pot cables. I was getting 0..1023 when I was doing just the example program AnalogInput but with the code I posted and my circuit I am getting very low values such as 15 for one of the pots and 0 for the other. When I divide by 4 I just get the PWM divided by 4 and no motor will move. When I do things like subtract 15 in the expression "analogWrite(enA, sensor1Value-15);" I get things like -15 going on and the other pot is no longer zero. I can multiply one of the pots to get numbers such as 250 and then they both usually work but fiddling with the pots changes both of the values in random ways. The numbers also fluctuate sometimes. They seem to be affecting each other. Usually one of them is on zero. They are wired in parallel, with negatives to the ground on the motor driver which is connected to ground on the arduino and the positives to the 5V output on the motor driver which is connected to the 5V on the arduino. I also tried one motor and pot at a time. The motors are right next to each other so could they have magnetic interference? The wires don't have iron rings on them. The arduino has been swapped for another. The pots have been swapped with others. Nothing is shorting as far as I can tell. All of the ENA, ENB, IN1, IN2, IN3, IN4 etc are in the correct place. Should i be using rotary encoders instead of pots? Do you know any links to coding and wiring instructions for what I want to do, which is to control the speed of two motors with Arduino, via PWM and only 1 battery? I don't even need to reverse direction. Could I use a MOSFET or something? other than the L298N. Probably my code or wiring is wrong.

Reply #5 is almost impossible to read becaue it has no paragraphs. Please don't make more long "stream of consciousness posts"

This sentence makes no logical sense.

Both motors are okay because I've swapped around pot cables.

How would swapping the connections to the pots tell you anything about motors.

This sentence seems to go to the heart of the problem

I was getting 0..1023 when I was doing just the example program AnalogInput but with the code I posted and my circuit I am getting very low values such as 15 for one of the pots and 0 for the other.

It is pointless wondering about anything else until your pots give you reliable data.

Make a pencil drawing showing how everything is wired up and post a photo of the drawing.

Write a short program that does nothing except read the pots and display the values in the Serial Monitor.

...R

Need to see your wiring. And what parts you're actually using. Are your pots identical? How much power does each motor need?

Hi,
A copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png, will help.
Looking at your picture, why are the pot wires going to the motor driver PCB?

Thanks.. Tom... :slight_smile:

Thanks for all of the questions and replies. I have got it working with the code below and I'm 99% sure the wiring was correct. All I did to the wiring was move the 5V connections to the breadboard and use all of the Arduino grounds instead of the ground on the driver (which was connected to the Arduino anyway). Correct me if this was my mistake please! Thanks Andrew Madsen and narner at serial - Problem with reading multiple potentiometer values on Arduino Uno - Arduino Stack Exchange. In the end I have no idea why the code did not work. The readings were bogus even when the driver board and motors were not connected.

Ultimately I want it to run very slowly and preferably quieter, so I'm thinking of one of the following options

  1. Adding a speed encoder to each motor or
  2. Changing to stepper motors (with speed encoders?).

I think steppers will be quieter but I would need two L298N drivers or something else, which isn't a big deal so long as it works.

Any thoughts?

THE CODE

int lastPotentiometerOneValue = 0;
int lastPotentiometerTwoValue = 0;

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;

void setup() {
// initialize serial communication at 57600 bits per second:
Serial.begin(57600);

// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}

void loop() {
delay(10);
readAndSendPotentiometerDataIfChanged();
//changemotorspeeds();
}

void readAndSendPotentiometerDataIfChanged(void) {

//Potentiometer One
int newPotentiometerOneValue = analogRead(A0) / 4;
if (newPotentiometerOneValue != lastPotentiometerOneValue) {
Serial.print("!pos1 ");
Serial.print(newPotentiometerOneValue);
Serial.print(";");
lastPotentiometerOneValue = newPotentiometerOneValue;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, newPotentiometerOneValue);
}

//Potentiometer Two
int newPotentiometerTwoValue = analogRead(A1) / 4;
if (newPotentiometerTwoValue != lastPotentiometerTwoValue) {
Serial.print("!pos2 ");
Serial.print(newPotentiometerTwoValue);
Serial.print(";");
lastPotentiometerTwoValue = newPotentiometerTwoValue;
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, newPotentiometerTwoValue);
}

//void changemotorspeeds(void) {

//Motor One
//digitalWrite(in1, HIGH);
//digitalWrite(in2, LOW);
//analogWrite(enA, newPotentiometerOneValue);

//Motor Two
//digitalWrite(in3, HIGH);
//digitalWrite(in4, LOW);
//analogWrite(enB, newPotentiometerTwoValue);
//}

}

Hi,
Good to see you have it working.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Since you don't change direction and these never change:-

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

You could put them in 'setup()', then forget about them and only worry about the EN inputs.

Checking to see if the pot value has changed before writing to the PWM pins doesn't really achieve anything either.
So in 'loop()', all you really need is:-

void loop()
{
    analogWrite(enA, analogRead(A0) / 4);
    analogWrite(enB, analogRead(A1) / 4);
}

Rather than 'int', these are best declared as 'const byte' too:-

//Motor One
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;