L9110s Speed and direction ESP32 with ps4 controller

Good day

I am controlling a ESP32 with L9110s H-Bridge. I am trying to control motor direction and speed. I managed to get direction working but not speed, can anybody help please?

#include <PS4Controller.h>

// Motor 
int rightMotorPin1=16;
int rightMotorPin2=17;


const int PWMFreq = 1000; /* 1 KHz */
const int PWMResolution = 8;
const int rightMotorPWMSpeedChannel = 4;


void notify()
{
  int rightMotorSpeed, leftMotorSpeed;
  rightMotorSpeed = map( PS4.LStickY(), -127, 127, -255, 255); //Left stick  - y axis - forward/backward left motor movement


  rightMotorSpeed = constrain(rightMotorSpeed, -255, 255);
  

  //Serial.println(rightMotorSpeed);
  //Serial.println(leftMotorSpeed);  
  rotateMotor(rightMotorSpeed);
}

void onConnect()
{
  Serial.println("Connected!.");
}

void onDisConnect()
{
  rotateMotor(0);
  Serial.println("Disconnected!.");    
}

void rotateMotor(int rightMotorSpeed)
{
  if (rightMotorSpeed < 0)
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,LOW);    
  }
  else if (rightMotorSpeed > 0)
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,HIGH);      
  }
  else
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,HIGH);      
  }
  ledcWrite(rightMotorPWMSpeedChannel, abs(rightMotorSpeed));
     
}

void setUpPinModes()
{
  
  pinMode(rightMotorPin1,OUTPUT);
  pinMode(rightMotorPin2,OUTPUT);
  


  //Set up PWM for motor speed
  ledcSetup(rightMotorPWMSpeedChannel, PWMFreq, PWMResolution);
   

   
  
  rotateMotor(0);
}


void setup()
{
  setUpPinModes();
  Serial.begin(115200);
  PS4.attach(notify);
  PS4.attachOnConnect(onConnect);
  PS4.attachOnDisconnect(onDisConnect);
  PS4.begin();
  Serial.println("Ready.");
}

void loop()
{
}

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Can you please post a link to where you got the PS4Controller library.

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

const int PWMFreq = 1000; /* 1 KHz */
const int PWMResolution = 8;
const int rightMotorPWMSpeedChannel = 4;

Below is the correct calculation of ESP32 Resolution.
// Calculation of adjustments for each frequency range
// Resolution = log2(Clock(80MHz)/f) + 1 ex: 50,000 HZ = 80,0000/50,000 = 1,600 log2(1600) = 10 + 1 = 11
// Duty 50% = (2 * * Resolution)/2 ex: 2**11 = 2048 2048/2 = 1024

Your project:
Clock..............Freq..........Clk/Freq
80,000,000...../...1,000....=....80,000
resolution = log2(80,000) = 16 + 1 = 17
Duty 2**17 = ~ 132,000

Duty 0 to 132,000

For your project, the resolution value should be 17, and the analogWrite should vary from 0 to 132,000.

Sorry for the late reply, Here is the link:PS4

Hi thank you for the reply. I am a big Noob, This is what I made of your suggestion

#include <PS4Controller.h>

// Motor 
int rightMotorPin1=16;
int rightMotorPin2=17;


const int PWMFreq = 80000; /* 1 KHz */
const int PWMResolution = 17;
const int rightMotorPWMSpeedChannel = 4;


void notify()
{
  int rightMotorSpeed, leftMotorSpeed;
  rightMotorSpeed = map( PS4.LStickY(), 0, 132000, 0, 132000); //Left stick  - y axis - forward/backward left motor movement


  rightMotorSpeed = constrain(rightMotorSpeed, -132000, 132000);
  

  //Serial.println(rightMotorSpeed);
  //Serial.println(leftMotorSpeed);  
  rotateMotor(rightMotorSpeed);
}

void onConnect()
{
  Serial.println("Connected!.");
}

void onDisConnect()
{
  rotateMotor(0);
  Serial.println("Disconnected!.");    
}

void rotateMotor(int rightMotorSpeed)
{
  if (rightMotorSpeed < 0)
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,LOW);    
  }
  else if (rightMotorSpeed > 0)
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,HIGH);      
  }
  else
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,HIGH);      
  }
  ledcWrite(rightMotorPWMSpeedChannel, abs(rightMotorSpeed));
     
}

void setUpPinModes()
{
  
  pinMode(rightMotorPin1,OUTPUT);
  pinMode(rightMotorPin2,OUTPUT);
  


  //Set up PWM for motor speed
  ledcSetup(rightMotorPWMSpeedChannel, PWMFreq, PWMResolution);
   

   
  
  rotateMotor(0);
}


void setup()
{
  setUpPinModes();
  Serial.begin(115200);
  PS4.attach(notify);
  PS4.attachOnConnect(onConnect);
  PS4.attachOnDisconnect(onDisConnect);
  PS4.begin();
  Serial.println("Ready.");
}

void loop()
{
}

I am 100% I got it wrong because it still reacts the same. I can change direction but not speed and also there is no stop, either forward or reverse no neutral.

Hi,
9V battery not a smokedetector battery is it?

If so then it is nowhere powerful enough for your project.

Can you please post some images of your project so we can see your components?

Have you got a DMM, Digital Multimeter?

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



I ditched the battery , using 12v power supply now. Yes I do have multimeter.

Hello, @barendzam

I was researching something else and ended up coming across your post.

Have you managed to solve it yet?

Even so, I will leave my contribution here:
Because I went through a similar situation to control DC motors.

There's a github that helped me a lot, after taking a look:

Diagram:

*in the part of the diagram, the L298N component, in addition to the connections for each motor, uses the ENA and ENB pins (to use these pins you need to remove the jumpers). With these pins I can control the speed of the motors.

ENA - Controls LEFT Motor speed
ENB - Controls RIGHT Motor speed

*The "Speed Control Pins" section explains these inputs to control speed.

image: https://lastminuteengineers.b-cdn.net/wp-content/uploads/arduino/L298N-Module-Speed-Control-Pins.png

Complete example code to control the motors (I used it as a basis for my project, this example works very well):

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