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()
{
}
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.
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.