[SOLVED] Pin always reads LOW when using digitalWrite in library

Hello everyone,

I'm new here, a bit confused and can't find the issue in my own code. Currently working on something like a remote controlled car.

My circuit is the following:

  • ESP32
  • 3-6V Motor
  • L298N Driver
  • 2*4,5V Battery

I ran a file I found online for testing the motor and it worked just fine. Then, I wanted to learn how to make a library and created one that you can use to control the motor. It is very similar to the code i found online. The only difference is that the motor I am using only turns backwards (which wasn't an issue before.) After some troubleshooting, I found that pin 26 always reads LOW, which is the reason for the motor never turning forward. I know it has to be an issue in the code, but I just don't see why it is never set to HIGH.

I've tried the following code and it works just fine:

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

// Motor A
int motor1Pin1 = 27; 
int motor1Pin2 = 26; 
int enable1Pin = 14; 

// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

void setup() {
  // sets the pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  
  // configure LED PWM functionalitites
  ledcSetup(pwmChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(enable1Pin, pwmChannel);

  Serial.begin(115200);

  // testing
  Serial.print("Testing DC Motor...");
}

void loop() {
  // Move the DC motor forward at maximum speed
  Serial.println("Moving Forward");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor backwards at maximum speed
  Serial.println("Moving Backwards");
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor forward with increasing speed
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  while (dutyCycle <= 255){
    ledcWrite(pwmChannel, dutyCycle);   
    Serial.print("Forward with duty cycle: ");
    Serial.println(dutyCycle);
    dutyCycle = dutyCycle + 5;
    delay(500);
  }
  dutyCycle = 200;
}

Then, I made said library:

#ifndef _MYMOTOR_H
#define _MYMOTOR_H
#include <Arduino.h>


class MyMotor
{
  public:
    MyMotor(String _motorName, int _pin1, int _pin2, int _enablePin, int _pwmChannel, int _freq = 30000,  int _resolution = 8);
    void start();
	void forward();
    void backwards();
    void stopMotor();
    void setDutyCycle(int dutyCycle);
  private:
    String motorName;
    int pin1;
    int pin2;
    int enablePin;

    //PWM:
    int freq;
    int pwmChannel;
    int resolution;
};

#endif
#include "Arduino.h"
#include <MyMotor.h>

    MyMotor::MyMotor(String _motorName, int _pin1, int _pin2, int _enablePin, int _pwmChannel, int _freq,  int _resolution){
      
	  motorName = _motorName;
      pin1 = _pin1;
      pin2 = _pin2;
      enablePin = _enablePin;

      //between 0 and 15:
      pwmChannel = _pwmChannel;

      //in Hertz:
      freq = _freq;

      //1-16 Bits
      resolution = _resolution;
    }

	void MyMotor::start(){
		//Assign Pins:
	  
	  // sets the pins as outputs:
	  pinMode(pin1, OUTPUT);
	  pinMode(pin1, OUTPUT);
	  pinMode(enablePin, OUTPUT);
  
	  // configure LED PWM functionalitites
	  ledcSetup(pwmChannel, freq, resolution);
	  
	  // attach the channel to the GPIO to be controlled
	  ledcAttachPin(enablePin, pwmChannel);
	}
	
    void MyMotor::forward(){
      Serial.println(motorName+" moving forward");
      digitalWrite(pin1, LOW);
      digitalWrite(pin2, HIGH); 

	  if (digitalRead(pin1) == HIGH) {
		  Serial.println("pin "+String(pin1)+" still HIGH");
	  }
	  if (digitalRead(pin2) == LOW) {
		  Serial.println("pin " + String(pin2) + " still LOW");
	  }
    }

    void MyMotor::backwards(){
      Serial.println(motorName+" moving backwards");
      digitalWrite(pin1, HIGH);
      digitalWrite(pin2, LOW); 

	  if (digitalRead(pin1) == LOW) {
		  Serial.println("pin " + String(pin1) + " still LOW");
	  }
	  if (digitalRead(pin2) == HIGH) {
		  Serial.println("pin " + String(pin2) + " still HIGH");
	  }
    }

    void MyMotor::stopMotor(){
      Serial.println(motorName+" stopped");
      digitalWrite(pin1, LOW);
      digitalWrite(pin2, LOW); 
    }

    void MyMotor::setDutyCycle(int dutyCycle){
      if (dutyCycle<0 | dutyCycle>= resolution*32){
        Serial.println("---ERROR: speed/dutycycle is "+String(dutyCycle) +", which is higher than dutycycle resolution ("+String(resolution)+")");
      }else{
        ledcWrite(pwmChannel, dutyCycle);  
      }
      
    }
    

Running the following code, the motor did not turn forward anymore:

#include <MyMotor.h>


int dutyCycle = 200;

MyMotor m = MyMotor("Motor A", 27, 26, 14, 0, 30000, 8);

void setup() {
  m.start();
  
  Serial.begin(115200);
}

void loop() {
  
  // Move the DC motor forward at maximum speed
  m.forward();

  delay(2000);


  
  // Stop the DC motor
  m.stopMotor();
  delay(2000);

  
  // Move DC motor backwards at maximum speed
  m.backwards();
  delay(2000);


  // Stop the DC motor
  m.stopMotor();
  delay(2000);

  // Move DC motor forward with increasing speed
  Serial.println("Increasing speed:");
  m.forward();
  while (dutyCycle <= 255){
    m.setDutyCycle(dutyCycle);
    dutyCycle = dutyCycle + 5;
    delay(500);
  }
  dutyCycle = 200;

  // Stop the DC motor
  m.stopMotor();
  delay(2000);
}

This is printed to the serial monitor:
Motor A moving backwards
Motor A stopped
Increasing speed:
Motor A moving forward
pin 26 still LOW
Motor A stopped
Motor A moving forward
pin 26 still LOW
Motor A stopped
Motor A moving backwards
Motor A stopped
Increasing speed:
Motor A moving forward
pin 26 still LOW

If you've even read so far, thanks a lot :joy:

Any help is appreciated:)

Copy and paste error?

Oh god. No words needed.
Thanks a lot for the insanely quick reply:)

I'm glad that I could help.

pin1 and pin2 are not very descriptive names, maybe you find better ones.

1 Like

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