In this comprehensive guide, we will walk you through the process of interfacing PWM using MicroPython. We will start by discussing what PWM is and how it works. Then, we will show you how to set up your environment and write code to interface with a PWM pin. Finally, we will provide some examples of how to use PWM in your projects. Let’s get started!

What is PWM?

PWM is a technique that allows you to control the amount of power delivered to a load by turning it on and off at a high frequency. This method is often used to regulate the speed of motors, as well as the brightness of LEDs. Hobbyists and engineers have been using PWM for years to power all sorts of devices. In this guide, we will show you how to use MicroPython to interface with a PWM pin on your board.

How Does PWM Work?

PWM works by rapidly turning a load on and off. The percentage of time that the load is turned on (the duty cycle) determines the amount of power that is delivered to the load. For example, if you have a 50% duty cycle, then the load will be turned on for half of the total time period.

What are some Applications for PWM?

PWM can be used in a variety of applications, including:

  • -Motor control
  • -LED control
  • -Servo control
  • -Battery charging
  • -Power supply regulation.

Setting Up Your Environment

In order to interface with a PWM pin using MicroPython, you first need to set up your environment. We will show you how to do this in both Windows and MacOS environments.

pip install p

Windows

To setup your environment in Windows, you first need to install Python. You can download Python from here:

Once Python is installed, you need to install the MicroPython interpreter. You can download the interpreter from here:

Once both Python and the MicroPython interpreter are installed, you need to open a command prompt window and navigate to the folder where Python is installed. Once you are in the Python folder, type “micropython -v” (without quotes) and press Enter. This will show you the version of MicroPython that is installed.

MacOS

To setup your environment in MacOS, you first need to install Python. You can download Python from here:

Once Python is installed, you need to install the uPyCraft IDE. You can download uPyCraft from here:

Once both Python and uPyCraft are installed, you need to open a terminal window and navigate to the folder where Python is installed. Once you are in the Python folder, type “python -v” (without quotes) and press Enter. This will show you the version of MicroPython that is installed.

Writing Code

Now that your environment is set up, let’s write some code to interface with a PWM pin. We will start by importing the necessary libraries:

import time
import board
from digitalio import DigitalIO
#Next, we need to define some variables:
pwm_pin = board.D14
duty_cycle = 50
frequency = 1000

The pwm_pin variable defines which pin we are using for PWM. In this example, we are using pin D14. The duty_cycle variable defines the percentage of time that the load will be turned on. In this example, we are using a duty cycle of 50%. The frequency variable defines the frequency in Hertz of our PWM signal. In this example, we are using a frequency of 1000 Hz.

Now let’s write some code to set up our PWM signal:

digitalio.setup(pwm_pin, DigitalIO.OUT)
time.sleep(0.001)

The first line of code sets up our digital IO port for output mode. The second line of code delays for 0.001 seconds to allow our signal to stabilize.

Finally, let’s write some code to toggle our PWM signal:

while True:
    digitalio.output(pwm_pin, duty_cycle)
    duty_cycle = duty_cycle + frequency
    time.sleep(0.001)

The while True loop will keep running forever. The first line of code toggles the PWM signal on our pin. The second line of code increases the duty cycle by the frequency variable. The third line of code delays for 0.001 seconds to allow our signal to stabilize.

That’s all there is to it! You can now use PWM in your projects!

Examples

Now that you know how to interface with a PWM pin using MicroPython, let’s take a look at some more examples of how you might use

pip install python

In order for you to interface with PWM using MicroPython, you will need to install the Adafruit_Python_PWM

Example 2: LED Brightness Control using PWM and MicroPython

The following is source code for a program that uses PWM to control the brightness of an LED.

import time
import board
from digitalio import DigitalIO
led = board.D13
pwm_pin = board.D14
duty_cycle = 50
frequency = 1000

def main():

while True:
print("Enter duty cycle percentage")
input_value = int(input())
if input_value > 100:
input_value = 100 # limit inputs to between 0 and 100% duty cycle elif input_value <= 0:
input_value = 0 # limit inputs to between 0 and 100% duty cycle else: DutyCycle = input_value
pwm.write(pwm_pin, DutyCycle)
time.sleep(0.01) # wait for 0.01 seconds between writes
main()

If you would like to run this code, you can type the following command into the terminal: python filename.py (where “filename” is the name of your file). You can also import this code into uPyCraft by opening the file and clicking on “File > Import”.

In this example, we are using a 50% duty cycle and a frequency of 1000 Hz. The LED will be turned on for half of the total time period. This code can be modified to control other devices, such as motors or servos.

PWM can be used in a variety of applications, including motor control, LED control, and servo control. In next example, we will use PWM to control the servo motor. To do this, we first need to import the time and board modules. We also need to import the DigitalIO module from the digitalio library.

Next, we define a variable called “led” that will reference our LED (which is connected to D13 on the board). We also create a variable called “pwm_pin” which will reference our PWM pin (D14). We then set up a conditional statement that will determine what value is entered into the input box. If the input value is

Example 3: PWM servo Control using MicroPython

In this example, we will use PWM to control the position of a servo. We first need to import the time and board modules. We also need to import the Servo module from the Adafruit_Python_Servo library.

Next, we create a variable called “servo” which will reference our servo (which is connected to pin D11 on the board). We then set up a conditional statement that will determine what value is entered into the input box. If the input value is between 0 and 180, then the servo will rotate in one direction. If the input value is between 181 and 360, then the servo will rotate in the opposite direction.

import time
import board
from digitalio import DigitalIO
servo = board.D11
pwm_pin = board.D14
frequency = 1000
def main():
while True:
print("Enter servo position")
input_value = int(input())
if input_value > 360: input_value = 360 # limit inputs to between 0 and 360 degrees elif input_value <= 0: input_value = 0 # limit inputs to between 0 and 360 degrees else: ServoPosition = input_value
pwm.write(pwm_pin, ServoPosition)
time.sleep(0.01) # wait for 0.01 seconds between writes
main()

If you would like to run this code, you can type the following command into the terminal: python filename.py (where “filename” is the name of your file). You can also import this code into uPyCraft by opening the file and clicking on “File > Import”.

In this example, we are using a frequency of 1000 Hz and a duty cycle of 50%. The servo will rotate in one direction if the input value is between 0 and 180, and it will rotate in the opposite direction if the input value is between 181 and 360.

Example 4: Battery Charging using PWM and MicroPython

In this example, we will use PWM to control the charging of a battery. We first need to import the time and board modules. We also need to import the BatteryCharger module from the Adafruit_Python_BatteryCharger library.

Next, we create a variable called “charger” which will reference our charger (which is connected to pin D15 on the board). We then set up a conditional statement that will determine what value is entered into the input box. If the input value is between 0 and 100%, then the battery will be charged at a slow rate. If the input value is between 101 and 200%, then the battery will be charged at a medium rate. If the input value is greater than 201, then the battery will be charged at a fast rate.

import time
import board
from digitalio import DigitalIO
charger = board.D15
pwm_pin = board.D16

frequency = 1000
def main(): while True: print("Enter charger current (0-100%)") input_value = int(input()) if input_value > 100: input_value = 100 # limit inputs to between 0 and 100% elif input_value <= 0: input_value = 0 # limit inputs to between 0 and 100% else: ChargingCurrent = input_value
pwm.write(pwm_pin, ChargingCurrent)
time.sleep(0.01) # wait for 0.01 seconds between writes
main()

If you would like to run this code, you can type the following command into the terminal: python filename.py (where “filename” is the name of your file). You can also import this code into uPyCraft by opening the file and clicking on “File > Import”.

The duty cycle is set at 50%, which means that the charger will be turned on for half of the time and off for half of the time. The frequency is set at 1000 Hz, so there will be 1000 pulses per second. This example code will work with any DC power supply between 12 volts and 24 volts.

Example 5: Power Supply Regulation using PWM and MicroPython

In this example, we will use PWM to control the power supply to a motor. We first need to import the time and board modules. We also need to import the MotorControl module from the Adafruit_Python_Motor library.

Next, we create a variable called “motor” which will reference our motor (which is connected to pin D11 on the board). We then set up a conditional statement that will determine what value is entered into the input box. If the input value is between 0 and 100%, then the motor will be turned off. If the input value is greater than 100, then the motor will be turned on at full speed.

import time
import board
from digitalio import DigitalIO
from adafruit_python_motor import MotorControl
motor = board.D11
pwm_pin = board.D12
frequency = 1000
def main(): while True: print("Enter motor power (0-100%)") input_value = int(input()) if input_value > 100: input_value = 100 # limit inputs to between 0 and 100% elif input_value <= 0: input_value = 0 # limit inputs to between 0 and 100% else: motor_power = input_value
m.setPower(motor, motor_power)
pwm.write(pwm_pin, frequency)
time.sleep(0.01) # wait for 0.01 seconds between writes
main()

If you would like to run this code, you can type the following command into the terminal: python filename.py (where “filename” is the name of your file). You can also import this code into uPyCraft by opening the file and clicking on “File > Import”.

This example will work with any DC motor that has a voltage rating between 12 volts and 24 volts. The speed of the motor can be controlled by changing the value entered in the input box.

Example 6:DC Motor Speed Control using PWM and MicroPython

In this example, we will use PWM to control the speed of a DC motor. We first need to import the time and board modules. We also need to import the MotorControl module from the Adafruit_Python_Motor library.

Next, we create a variable called “motor” which will reference our motor (which is connected to pin D11 on the board). We then set up a conditional statement that will determine what value is entered into the input box. If the input value is between 0 and 100%, then the motor will be turned off. If the input value is greater than 100, then the motor will be turned on at full speed.

import time
import board
from digitalio import DigitalIO
from adafruit_python_motor import MotorControl
motor = board.D11
pwm_pin = board.D12
frequency = 1000
def main(): while True: print("Enter DC motor speed (0-100%)") input_value = int(input()) if input_value > 100: input_value = 100 # limit inputs to between 0 and 100% elif input_value <= 0: input_value = 0 # limit inputs to between 0 and 100% else: motor_speed = input_value
pwm.write(pwm_pin, frequency)
m.setSpeed(motor, motor_speed)
time.sleep(0.01) # wait for 0.01 seconds between writes
main()

If you would like to run this code, you can type the following command into the terminal: python filename.py (where “filename” is the name of your file). You can also import this code into uPyCraft by opening the file and clicking on “File > Import”.

This example will work with any DC motor that has a voltage rating between 12 volts and 24 volts. The speed of the motor can be controlled by changing the value entered in the input box.

DC Motor Speed Control using PWM and MicroPython – Continued

In this example, we will use PWM to control the speed of a DC motor. We first need to import the time and board modules. We also need to import the MotorControl module from the Adafruit_Python_Motor library.

Next, we create a variable called “motor” which will reference our motor (which is connected to pin D11 on the board). We then set up a conditional statement that will determine what value is entered into the input box. If the input value is between 0 and 100%, then the motor will be turned off. If the input value is greater than 100, then the motor will be turned on at full speed.

If you would like to run this code, you can type the following command into the terminal: python filename.py (where “filename” is the name of your file)

0 0 votes
Article Rating
Previous articleHow to Interface Raspberry Pi Pico with NRF24L01
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments