Skip to content

Commit

Permalink
Merge pull request #3 from ElectroMagus/featureupdate
Browse files Browse the repository at this point in the history
Feature update for new release
  • Loading branch information
ElectroMagus authored Jun 24, 2022
2 parents 72cf2dc + 5aa3398 commit 0128bbb
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 25 deletions.
13 changes: 13 additions & 0 deletions ESP32MX1508.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ MX1508::MX1508( uint8_t pinIN1, uint8_t pinIN2, uint8_t ledCH1, uint8_t ledCH2)
pinMode(_pinIN2, OUTPUT);
ledcSetup(_ledCH2, 2500, 8); // Setup channel at 2500Hz with 8 bit (0-255) resolution
ledcAttachPin(_pinIN2, _ledCH2);
_maxpwm = 255; // Sets a flag on the motor so the object knows the max pwm value
}

MX1508::MX1508( uint8_t pinIN1, uint8_t pinIN2, uint8_t ledCH1, uint8_t ledCH2, uint8_t resolution) {
Expand All @@ -24,6 +25,9 @@ MX1508::MX1508( uint8_t pinIN1, uint8_t pinIN2, uint8_t ledCH1, uint8_t ledCH2,
pinMode(_pinIN2, OUTPUT);
ledcSetup(_ledCH2, 2500, resolution); // Setup channel at 2500Hz with 8 (0-255), 12 (0-4095), or 16 (0-65535) bit resolution
ledcAttachPin(_pinIN2, _ledCH2);
if (resolution == 8) { _maxpwm = 255; } // Sets a flag on the motor so the object knows the max pwm value
if (resolution == 12) { _maxpwm = 4095; }
if (resolution == 16) { _maxpwm = 65535; }
}

MX1508::MX1508( uint8_t pinIN1, uint8_t pinIN2, uint8_t ledCH1, uint8_t ledCH2, uint8_t resolution, long freq) {
Expand All @@ -37,6 +41,9 @@ MX1508::MX1508( uint8_t pinIN1, uint8_t pinIN2, uint8_t ledCH1, uint8_t ledCH2,
pinMode(_pinIN2, OUTPUT);
ledcSetup(_ledCH2, freq, resolution); // Setup channel at specified Hz with 8 (0-255), 12 (0-4095), or 16 (0-65535) bit resolution
ledcAttachPin(_pinIN2, _ledCH2);
if (resolution == 8) { _maxpwm = 255; } // Sets a flag on the motor so the object knows the max pwm value
if (resolution == 12) { _maxpwm = 4095; }
if (resolution == 16) { _maxpwm = 65535; }
}

void MX1508::stopMotor() { // Kept for backwards compatibility
Expand All @@ -49,6 +56,12 @@ void MX1508::motorStop() {
ledcWrite(_ledCH2, 0);
}

void MX1508::motorBrake() {
ledcWrite(_ledCH1, _maxpwm);
ledcWrite(_ledCH2, _maxpwm);

}

void MX1508::motorGo(long pwmSpeed) {
_pwmVal = pwmSpeed;
ledcWrite(_ledCH1, _pwmVal);
Expand Down
4 changes: 3 additions & 1 deletion ESP32MX1508.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class MX1508 {
MX1508(uint8_t pinIN1, uint8_t pinIN2, uint8_t ledCH1, uint8_t ledCH2, uint8_t resolution, long freq);
void motorGo(long pwmVal);
void motorRev(long pwmVal);
void motorStop();
void motorStop(); // Sets both signals to low to allow motor to spin down
void motorBrake(); // Sets boths signals to high to hard brake the motor
void stopMotor(); // Kept for backwards compatibility


Expand All @@ -22,6 +23,7 @@ class MX1508 {
uint8_t resolution; // PWM Resolution
long freq; // PWM Freq
long _pwmVal; // PWM Value (speed)
long _maxpwm; // Max PWM Value of the Motor
};

#endif
67 changes: 46 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,75 @@

# MX1508 Driver Board Library for ESP32/Arduino

Arduino library for MX1508 dual DC motor driver compatible with the ESP32.

This was originally from the wonderful AVR Library at https://github.com/Saeterncj/MX1508 This is my first library, so I'm also using their project as a skeleton for the extra information.


This was inspired by [this wonderful AVR Library](https://github.com/Saeterncj/MX1508). This is my first library, so please feel free to provide feedback.

It was modified for the ESP32 to use the LEDC Driver to send the PWM signals for speed control. Since the ESP32 can multiplex many of its pin functions, this library creates PWM Channels and assigns them to the pins specified.
The MX1508 Motor Driver is a nice, low cost motor driver works from 2-10VDC, with a 1.5A operating current (2A peak). The driver is ideal for battery powered, smaller DC motors.

The ESP32 to use the LEDC Driver to send the PWM signals for speed control. The ESP32 can multiplex many of its pin functions, this library creates PWM Channels and assigns them to the pins specified.

The LEDC driver supports 8, 12 and 16 bit PWM resolution and can operate at a variety of frequencies allowing you to tune the driver to your particular motor.




## Objective
This library is made to simplify the use of MX1508. This Arduino Libary only requires a few lines of code to
start controlling DC motors with mx1508. Resolutions of 8, 12, and 16 bits are supported for control of the motor speed.


This library is made to simplify the use of MX1508 by allowing for the simple setup of motors and then using functions to drive them.
```
#include <ESP32MX1508.h>
#define PINA 9
#define PINB 10
#define CH1 0 // 16 Channels (0-15) are availible
#define CH2 1 // Make sure each pin is a different channel and not in use by other PWM devices (servos, LED's, etc)
#define CH1 0 // 16 Channels (0-15) are availible
#define CH2 1 // Make sure each pin is a different channel and not in use by other PWM devices (servos, LED's, etc)
// Optional Parameters
#define RES 8 // Resolution in bits: 8 (0-255), 12 (0-4095), or 16 (0-65535)
#define FREQ 5000 // PWM Frequency in Hz
#define RES 8 // Resolution in bits: 8 (0-255), 12 (0-4095), or 16 (0-65535)
#define FREQ 5000 // PWM Frequency in Hz
MX1508 motorA(PINA,PINB, CH1, CH2); // Default- 8 bit resoluion at 2500 Hz
//MX1508 motorA(PINA,PINB, CH1, CH2, RES); // Specify resolution
//MX1508 motorA(PINA,PINB, CH1, CH2, RES, FREQ); // Specify resolution and frequency
MX1508 motorA(PINA,PINB, CH1, CH2); // Default- 8 bit resoluion at 2500 Hz
//MX1508 motorA(PINA,PINB, CH1, CH2, RES); // Specify resolution
//MX1508 motorA(PINA,PINB, CH1, CH2, RES, FREQ); // Specify resolution and frequency
void setup() {}
void setup() {
Serial.begin(9600);
}
void loop() {
motorA.motorGo(200); // Pass the speed to the motor: 0-255 for 8 bit resolution
delay(100);
motorA.motorStop(); // Stop no argument
motorA.motorStop(); // Soft Stop -no argument
delay(100);
motorA.motorRev(200); // Pass the speed to the motor: 0-255 for 8 bit resolution
delay(100);
MotorA.motorBrake(); // Hard Stop -no arguement
}
```

## Methods and Functions
+ motorGo(pwmVal)
- This sets the PWM val which is related to the speed, depending on selected resolution
+ motorRev(pwmVal)
- This sets the PWM val which is related to the speed, depending on selected resolution
+ motorStop()
- Simply stops the motor



## Methods and Functions

+ motorGo(pwmVal)

- This sets the PWM val which is related to the speed, depending on selected resolution

+ motorRev(pwmVal)

+ This sets the PWM val which is related to the speed, depending on selected resolution

+ motorStop()

+ Stops the motor from moving in the current direction

+ motorBrake()

- Actively brakes the motor
3 changes: 2 additions & 1 deletion examples/basic/basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ void setup() {}
void loop() {
motorA.motorGo(200); // Pass the speed to the motor: 0-255 for 8 bit resolution
delay(100);
motorA.motorStop(); // Stop no argument
motorA.motorStop(); // Soft Stop -no argument
delay(100);
motorA.motorRev(200); // Pass the speed to the motor: 0-255 for 8 bit resolution
delay(100);
MotorA.motorBrake(); // Hard Stop -no arguement
}
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ ESP32MX1508 KEYWORD1

motorGo KEYWORD2
motorRev KEYWORD2
motorStop KEYWORD2
motorBrake KEYWORD2
stopMotor KEYWORD2


Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESPMX1508",
"version": "1.0.4",
"version": "1.0.5",
"description": "An ESP32 Arduino Library for the MX1508 Motor Driver Board",
"keywords": "mx1508, esp32, arduino",
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP32MX1508
version=1.0.4
version=1.0.5
author=Richard Salmon (ElectroMagus)
maintainer=Richard Salmon (ElectroMagus)
sentence=An ESP32 library that makes using mx1508 dual DC motor driver easier.
Expand Down

0 comments on commit 0128bbb

Please sign in to comment.