PWM pins not working or motor not working

I'm using PWM pins 5, 6, 9, and 10 to control the L298n motor. I'm sure that I get to the section in my code where analogWrite(PIN_PWM_L1,64); runs, but the car is not responding. I haven't touched this project for a couple months and it worked with the exact same configuration (almost positive).

How can I tell whether it's the motor or the pins that aren't working?

This is the code file, sorry if it's hard to read. It came with code for other functions that I'm not using but I can't remove or the code breaks.


//WIRING: in1 - D5, in2 - D6, in3 - D9, in4 - D10
//analog joystick values (with pin definitions on left): left to right - x-axis 0 to 1000, up to down - y-axis 0 to 1000 
// PIN_PWM_L1,PIN_PWM_L2,PIN_PWM_R1,PIN_PWM_R2  Low-level control of left DC motors via PWM 
//------------------------------------------------------//
// DEFINITIONS
//------------------------------------------------------//
//------------------------------------------------------//
//SETTINGS
//------------------------------------------------------//
// WARNING: If the sonar sensor is not setup, the car will go full speed forward!
#define NO_PHONE_MODE 1
//------------------------------------------------------//
// PINOUT
//------------------------------------------------------//
//Setup the pin definitions
#define PIN_PWM_L1 5 //left wheels forward
#define PIN_PWM_L2 6 //left wheels back
#define PIN_PWM_R1 10 //right wheels forward
#define PIN_PWM_R2 9 //right wheels back
#define X_pin A1 //joystick x-direction
#define Y_pin A0 //joystick y-direction
//------------------------------------------------------//
// INITIALIZATION
//------------------------------------------------------//
#include <limits.h>
const unsigned int STOP_THRESHOLD = 32; //cm
#if NO_PHONE_MODE
  int turn_direction = 0; // right
  const unsigned long TURN_DIRECTION_INTERVAL = 2000; // How frequently to change turn direction (ms).
  unsigned long turn_direction_timeout = 0;   // After timeout (ms), random turn direction is updated.
#endif
//Vehicle Control
int ctrl_left = 0;
int ctrl_right = 0;
//Voltage measurement
const unsigned int VIN_ARR_SZ = 10;
unsigned int vin_counter = 0;
unsigned int vin_array[VIN_ARR_SZ]={0};
//Speed sensor
const unsigned int DISK_HOLES = 20;
volatile int counter_left = 0;
volatile int counter_right = 0;
//Indicator Signal
const unsigned long INDICATOR_INTERVAL = 500; //Blinking rate of the indicator signal (ms).
unsigned long indicator_timeout = 0;
int indicator_val = 0;
//Serial communication
const unsigned long SEND_INTERVAL = 1000; // How frequently vehicle data is sent (ms).
unsigned long send_timeout = 0;
String inString = "";
//------------------------------------------------------//
// SETUP
//------------------------------------------------------//
void setup()
{
  //Outputs
  pinMode(PIN_PWM_L1,OUTPUT);
  pinMode(PIN_PWM_L2,OUTPUT);
  pinMode(PIN_PWM_R1,OUTPUT);
  pinMode(PIN_PWM_R2,OUTPUT);
  //Inputs
 
  Serial.begin(115200,SERIAL_8N1); //8 data bits, no parity, 1 stop bit
  send_timeout = millis() + SEND_INTERVAL; //wait for one interval to get readings
  
  #if HAS_SPEED_SENSORS
    attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(PIN_SPEED_L), update_speed_left, FALLING);
    attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(PIN_SPEED_R), update_speed_right, FALLING);
  #endif  
}
//------------------------------------------------------//
// MAIN LOOP
//------------------------------------------------------//
void loop() {
  
  float joy_x = analogRead(X_pin); //read data to joystick variables
  float joy_y = analogRead(Y_pin);
  
  // Send vehicle measurments to serial every SEND_INTERVAL
  if (millis() >= send_timeout) {
    send_vehicle_data();
    send_timeout = millis() + SEND_INTERVAL;
  }
    // drive forward
      Serial.println("joy x: "+String(joy_x));
      Serial.println("joy y: "+String(joy_y));
      if(joy_x>450&&joy_x<530) //neutral x is 504
      {
        if(joy_y >500 && joy_y<550) //if both x and y are centered, car does not move
        {
//          analogWrite(PIN_PWM_L1,0);
//          analogWrite(PIN_PWM_L2,0);
//          analogWrite(PIN_PWM_R1,0);
//          analogWrite(PIN_PWM_R2,0);
          ctrl_left=0;
          ctrl_right=0;
          Serial.println("not moving");
        }
        if(joy_y < 500) //if x is centered but y<500, car moves forwards
        {
          ctrl_left = 1;
          ctrl_right = 1;
          Serial.println("going forwards");
        }else if(joy_y>550) //neutral y is 523 - if x is centered but y>550, car moves back
        {
          ctrl_left = -1;
          ctrl_right = -1;
          Serial.println("going backwards");
        }
      }else if(joy_x<450) 
      {
          ctrl_left = 1;
          ctrl_right = 0;
          Serial.println("turn left");
      }else if(joy_x>530)
      {
        ctrl_left = 0;
        ctrl_right = 1;
        Serial.println("turn right");
      }
  update_left_motors();
  update_right_motors();
}
//------------------------------------------------------//
// FUNCTIONS
//------------------------------------------------------//
//write ctrl_left and ctrl_right values to motor
void update_left_motors() {
    if (ctrl_left < 0) {
      analogWrite(PIN_PWM_L1,64); 
    }
    else if (ctrl_left > 0) {
      analogWrite(PIN_PWM_L2,10);
    }
    else { //Motor brake
      analogWrite(PIN_PWM_L1,255);
      analogWrite(PIN_PWM_L2,255);
    }
}
void update_right_motors() {
    if (ctrl_right < 0) {
      analogWrite(PIN_PWM_R1,64);
    }
    else if (ctrl_right > 0) {
      analogWrite(PIN_PWM_R2,10);
    }
    else { //Motor brake
      analogWrite(PIN_PWM_R1,255);
      analogWrite(PIN_PWM_R2,255);
    }
}
//this segment came with openbot code, code breaks if removed
void send_vehicle_data() {
//  float voltage_value = get_voltage();
  int ticks_left = counter_left;
  counter_left = 0;
  int ticks_right = counter_right;
  counter_right = 0;
  
  #if (NO_PHONE_MODE || HAS_OLED)
    float rpm_factor = 60.0*(1000.0/SEND_INTERVAL)/(DISK_HOLES);
    float rpm_left = ticks_left*rpm_factor;
    float rpm_right = ticks_right*rpm_factor;
  #endif
  #if (NO_PHONE_MODE)
  //  Serial.print("Voltage: "); Serial.println(voltage_value, 2);
    Serial.print("Left RPM: "); Serial.println(rpm_left, 0);
    Serial.print("Right RPM: "); Serial.println(rpm_right, 0);
//    Serial.print("Distance: "); Serial.println(distance_estimate);
    Serial.println("------------------");
  #else
//    Serial.print(voltage_value);
//    Serial.print(",");
//    Serial.print(ticks_left);
//    Serial.print(",");
//    Serial.print(ticks_right);
//    Serial.print(",");
//    Serial.print(distance_estimate);
//    Serial.println();
  #endif 
}

By using your digital volt meter to measure the voltages!
Paul

1 Like

I do not have the time to take the code breaks etc out of your code. The schematic you posted, not a frizzy thing is great, I can almost see it. The L298n is a old device and is not a good choice.

everything worked perfectly fine a couple months ago...
Here's a video, it had trouble going straight but it worked lol

At risk of stating the obvious, why don't you try diagnosing the problem? If it worked two months ago, and doesn't now, then it's not going to be a software problem (unless you've accidentally changed it).

Start by measuring the basics: power supply voltages to the motors, drivers and Arduino. Put a voltmeter or oscilloscope on the PWM outputs to see what they are doing. Ditto for the driver outputs to the motor terminals.

You've given us nothing that could possibly help us diagnose the problem.

1 Like

Hi, @sensitive_cheater

Do you have a DMM?
Can I suggest you write some code to JUST make your motors run backwards and forwards. if you do not find a hardware problem?

Can you please tell us your electronics, programming, arduino, hardware experience?

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

I know for sure that the code gets to the analogWrite(PWM_PIN,value); sections. I don't have a DMM.

An update is that I borrowed a brand new Arduino from work and still have the same problem. I also replaced the wires connecting to the PWM inputs, so the problem must be with the motor. Is it possible that it won't run if there isn't enough power in the batteries? Should I get a new motor?

Currently in 2nd year computer engineering, took 2 circuits classes, 2 programming classes including data structures and algorithms, did a couple arduino projects in the past (this is the third).

UPDATE: doesn't work with fully charged batteries

Sorry I don't have access to an oscilloscope or voltmeter.

I would recommend a multimeter, even a cheap one would save you more money then purchasing and replacing the parts on a guess by guess basis.

Surely your college has a voltmeter or oscilloscope you could use?

Can you drive the motor direct from the battery?

Have you code that just drives the motor?

Tom... :smiley: :coffee: :coffee::coffee: :australia:

Sorry for the late reply. I got a multimeter from work and the result seems to make sense. I have the 'duty' needle touching my pwm pin, and the 'COM' needle touching ground. The base voltage is 5 volts, and when I write to the PWM pin (via joystick), the voltage goes up to ~200V.

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