Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
Add PWM_StepperControl example
Browse files Browse the repository at this point in the history
Credits of Paul van Dinther (https://github.com/dinther). Check #16
  • Loading branch information
khoih-prog authored Jan 21, 2023
1 parent b3d063a commit 2e81728
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions PWM_StepperControl/PWM_StepperControl.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/****************************************************************************************************************************
PWM_StepperControl.ino
For RP2040 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RP2040_PWM
Licensed under MIT license
The RP2040 PWM block has 8 identical slices. Each slice can drive two PWM output signals, or measure the frequency
or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven
by the PWM block
Credits of Paul van Dinther (https://github.com/dinther). Check https://github.com/khoih-prog/RP2040_PWM/issues/16
*****************************************************************************************************************************/

#define _PWM_LOGLEVEL_ 1

#if ( defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \
defined(ARDUINO_GENERIC_RP2040) ) && defined(ARDUINO_ARCH_MBED)

#if(_PWM_LOGLEVEL_>3)
#warning USING_MBED_RP2040_PWM
#endif

#elif ( defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \
defined(ARDUINO_GENERIC_RP2040) ) && !defined(ARDUINO_ARCH_MBED)

#if(_PWM_LOGLEVEL_>3)
#warning USING_RP2040_PWM
#endif
#else
#error This code is intended to run on the RP2040 mbed_nano, mbed_rp2040 or arduino-pico platform! Please check your Tools->Board setting.
#endif

#include <RP2040_PWM.h>

RP2040_PWM* stepper;
float frequency;
float dutyCycle;
#define STEP_PIN 8
#define DIR_PIN 9

void setSpeed(int speed)
{
// Set the frequency of the PWM output and a duty cycle of 50%
digitalWrite(DIR_PIN, (speed < 0));
stepper->setPWM(STEP_PIN, abs(speed), 50);
}

void setup()
{
pinMode(DIR_PIN, OUTPUT);

Serial.begin(115200);

while (!Serial && millis() < 5000);

delay(100);

Serial.print(F("\nStarting PWM_DynamicFreq on "));
Serial.println(BOARD_NAME);
Serial.println(RP2040_PWM_VERSION);

// Create PWM object and passed just a random frequency of 500 in it
// The duty cycle is how you turn the motor on and off
stepper = new RP2040_PWM(STEP_PIN, 500, 0);
}

void loop()
{
setSpeed(1000);
delay(3000);
setSpeed(-500);
delay(3000);
}

0 comments on commit 2e81728

Please sign in to comment.