Posted on 1 Comment

How to use MG90S Servo Motor with Raspberry Pi Pico using micropython

I have this MG90S small servo motor. It is supposed to go from 0° to 180°.

Because of their low cost, they have a short range which is less than 180°. Some go to 135°, 100° or 150°.

PWM signal required by servo to move.

Frequency = 50Hz
Time Period = 0.02 Second or 20 mili second
Pulse width range: 500us to 2500us

Properties of pulse width
At 500us pulse width the servo position will be 0°
At 2500us pulse width the servo position will be 180°

It is a good habit to check the servo controls before putting it in a project and make the adjustments in code or in hardware.

Hardware Connections

Raspberry Pi PicoServo Motor
GNDGND (Brown color wire)
VsysVCC (Red color wire)
GP9Signal (Orange color wire)

Most small and micro Servo operate from 5V to 6V.
If a servo is designed to operate at 3.3V always check it’s datasheet carefully.

Calculations

Raspberry pi pico has a 16 bit PWM controller.
This means we set its frequency to 50Hz by
pwm.freq(50)
This means that one oscillation has 65535 steps

65535 steps = 20 ms

First we convert angles to pulse width time

pulse width = angle/90 + 0.5
then we convert pulse width to step count

steps count = ( 65535 * pulse width ) / 20

Code

import machine
import utime

Led_pin = 25
LED = machine.Pin(Led_pin, machine.Pin.OUT)

# Configure the GPIO pin for PWM
pwm_pin = machine.Pin(9)
pwm = machine.PWM(pwm_pin)

# Set the PWM frequency to 50 kHz
pwm.freq(50)

# Debug Message Print
debug = 0

time_delay = 1/10
increment_step = 100
decrement_step = -200

'''
# Function: deg_to_time
# Parameters: deg : degree (0 - 180)
# Description: This function takes the degree and translates them
                to Pulse Width time.
                For a servo motor we have to use a pulse width of 500us to 2500us

'''
def deg_to_time(deg):
    temp = ((deg/90)+0.5)
    if debug:print("deg ",deg," to timems: ",temp)
    return temp

'''
# Function: timems_to_duty
# Parameters: timems : pulse width time in milli second
# Description: This function takes pulse width duration of 500us to 2500us.
                and generates a duty cycle value.

'''
def timems_to_duty(timems):
    temp = 20/timems
    temp1 = 65535/temp
    if debug:print("timems to duty: ",temp1)
    return temp1

'''
# Function: set_pwm
# Parameters: duty : duty cycle value (0 - 65535)
# Description: This function takes a duty cycle value and set it for the PWM.

'''
def set_pwm(duty):
    pwm.duty_u16(int(duty))
    if debug:print("duty cycle: ",duty)
    
def angle_to_pwm(angle):
    set_pwm(int(timems_to_duty(deg_to_time(angle))))



while(1):
        # sweep from 0  to 180
    for _ in range(0,180,1):
        angle_to_pwm(_)
        utime.sleep(time_delay)
        # sweep from 180  to 0
    for _ in range(180,0,-1):
        angle_to_pwm(_)
        utime.sleep(time_delay)
Posted on 1 Comment

Variables and Data Types in MicroPython on Raspberry Pi Pico

Variables and data types are fundamental concepts in programming. In MicroPython, just like in any programming language, variables are used to store data values and data types define the kind of data that can be stored.

In this tutorial, we will explore variables and data types in MicroPython on Raspberry Pi Pico.

Variables in MicroPython

Variables in MicroPython are used to store data values such as numbers, strings, and boolean values. A variable is simply a named reference to a value.

To create a variable, you simply assign a value to a name using the equals sign (=). For example:

x = 5

In this case, the variable x is assigned the value of 5.

You can also assign values to multiple variables at once, like this:

x, y, z = 5, "Hello", True

This assigns the value of 5 to the variable x, the string “Hello” to the variable y, and the boolean value True to the variable z.

Further Explanation

x, y, z = 5, “Hello”, True is a way of assigning values to multiple variables at once in Python. In this case, the values 5, “Hello”, and True are assigned to the variables x, y, and z respectively.

This syntax is known as “tuple unpacking”. The values on the right-hand side of the equals sign are packed into a tuple, which is then unpacked and assigned to the variables on the left-hand side.

Essentially, it’s the same as writing:

my_tuple = (5, "Hello", True)
x = my_tuple[0]
y = my_tuple[1]
z = my_tuple[2]

But using tuple unpacking is a more concise and readable way to assign values to multiple variables at once.

Data Types in MicroPython

MicroPython has several built-in data types that define the kind of data that can be stored in a variable. These include:

  • Integer: whole numbers, such as 5 or -3.
  • Float: decimal numbers, such as 3.14 or -2.5.
  • String: a sequence of characters, such as “Hello” or “123”.
  • Boolean: a value that is either True or False.
  • List: a collection of values, such as [1, 2, 3] or [“apple”, “banana”, “orange”].
  • Tuple: a collection of values, like a list, but cannot be modified once created, such as (1, 2, 3) or (“apple”, “banana”, “orange”).
  • Dictionary: a collection of key-value pairs, such as {“name”: “John”, “age”: 30}.

To determine the data type of a variable, you can use the type() function, like this:

This will output <class ‘int’>, indicating that x is an integer.

x = 5
print(type(x))  # Output: <class 'int'>