Deep Dive Into Servo Motor Programming with Raspberry Pi



Basics of Servo Motor Control

Pulse width modulation (PWM) is a technique used to control the amount of power delivered to a device by rapidly switching a signal between ON and OFF states. This allows for precise control of the average voltage or current over time. PWM is commonly used in electronic devices to control the speed of motors, the intensity of LEDs, and the position of servo motors.

The ON and OFF states of the signal are known as the pulse and the space, respectively. The ratio between the pulse and space is known as the duty cycle. The duty cycle represents the percentage of time the signal is in the ON state compared to the total time of one cycle. For example, a signal with a 50% duty cycle means the pulse and the space are equal in duration.

The frequency of a PWM signal refers to the number of cycles that occur in one second. It is measured in hertz (Hz) and can range from a few Hz to several kilohertz (kHz) or even megahertz (MHz). The frequency of the signal determines how rapidly the ON and OFF states are switched, and thus, how quickly the average power is controlled.

To control a servo motor using PWM, a microcontroller or single-board computer such as the Raspberry Pi, can generate a PWM signal and send it to the servo as a control signal. The servo motor then reads the PWM signal, determines the appropriate angle, and moves to that position accordingly.

To write a Python script to control a servo motor, we would need to use the “RPi.GPIO” library, which provides functions for controlling the GPIO pins on the Raspberry Pi. The following is a simple code snippet that uses PWM to control a servo motor:

```
import RPi.GPIO as GPIO
import time
```

Setup GPIO pins:
We need to setup the GPIO mode and define which pins we will be using for the servo motors.

```
GPIO.setmode(GPIO.BOARD)

# Define the pins for each servo motor
servo_pin_1 = 12
servo_pin_2 = 16
GPIO.setup(servo_pin_1, GPIO.OUT)
GPIO.setup(servo_pin_2, GPIO.OUT)
```

Initialize PWM (Pulse Width Modulation):
To control the angle of the servo motor, we need to use PWM. We will initialize PWM with a frequency of 50Hz and set the initial duty cycle to 0.

```
pwm_1 = GPIO.PWM(servo_pin_1, 50)
pwm_1.start(0)

pwm_2 = GPIO.PWM(servo_pin_2, 50)
pwm_2.start(0)
```

Function to set servo angle:
We will define a function that takes in the servo pin and the angle as parameters, and then calculates and sets the duty cycle to rotate the servo motor to the desired angle.

```
def set_angle(servo, angle):
duty = angle / 18 + 2
servo.ChangeDutyCycle(duty)
time.sleep(1)
servo.ChangeDutyCycle(0)
```

Control servo motors:
Now we can use the set_angle function to control the servo motors. For example, to set servo 1 to 90 degrees and servo 2 to 180 degrees, we can use the following code:

```
set_angle(pwm_1, 90)
set_angle(pwm_2, 180)
```

Clean up:
After using the servo motors, we should clean up the GPIO pins to avoid any conflicts with other programs.

```
GPIO.cleanup()
```

Full code:
```
import RPi.GPIO as GPIO
import time

# Setup GPIO pins
GPIO.setmode(GPIO.BOARD)
servo_pin_1 = 12
servo_pin_2 = 16
GPIO.setup(servo_pin_1, GPIO.OUT)
GPIO.setup(servo_pin_2, GPIO.OUT)

# Initialize PWM
pwm_1 = GPIO.PWM(servo_pin_1, 50)
pwm_1.start(0)

pwm_2 = GPIO.PWM(servo_pin_2, 50)
pwm_2.start(0)

# Function to set servo angle
def set_angle(servo, angle):
duty = angle / 18 + 2
servo.ChangeDutyCycle(duty)
time.sleep(1)
servo.ChangeDutyCycle(0)

# Control servo motors
set_angle(pwm_1, 90)
set_angle(pwm_2, 180)

# Clean up
GPIO.cleanup()
```

Controlling Servo Motors with Raspberry Pi GPIO

Introduction to Raspberry Pi GPIO pins: The Raspberry Pi has a total of 40 GPIO pins, which are small holes found on the edge of the board. These pins can be used as digital input or output, analog input or output, or as a serial interface. The pins are labeled with numbers and can be accessed through various programming languages, including Python, C, and Scratch.

GPIO libraries for Python programming: Python is a popular programming language used for Raspberry Pi projects due to its simple syntax and the large number of libraries available for GPIO programming. Some of the commonly used libraries for GPIO in Python include RPi.GPIO, WiringPi, and pigpio. These libraries provide an easy-to-use interface for controlling GPIO on the Pi and handle the low-level operations necessary for reading and writing to the pins.

Step-by-step guide for wiring and controlling servo motors with GPIO: Servo motors are widely used in robotics and hobby projects for their precise movement and easy control. They have three wires: power (typically red), ground (usually black or brown), and signal (usually white or yellow). The signal line is used to send control signals to the motor, which determines its position.

To wire a servo motor to the Raspberry Pi, connect the power wire to the 5V pin on the Pi, the ground wire to a ground pin, and the signal wire to a GPIO pin. The exact pin used for the signal wire can be specified in code when using a GPIO library.

To control the servo motor using Python and GPIO, follow these steps:

  • Import the necessary libraries: Import the GPIO library and the time library for a delay in code.

  • Set the GPIO mode: Set the GPIO mode to either BCM or BOARD, depending on the pin numbering system you prefer.

  • Set up the GPIO pin: Use the GPIO library to set up the GPIO pin as an output by using the GPIO.setup() function. This will also set the output of the pin to low initially.

  • Create a PWM object: Create a PWM (Pulse Width Modulation) object using the GPIO.PWM() function, passing in the GPIO pin and the frequency of the PWM signal as parameters. This will allow us to control the position of the servo motor by changing the duty cycle of the PWM signal.

  • Start the PWM signal: Use the PWM.start() function to start the PWM signal with a duty cycle of 0. This will allow the servo motor to remain at its default position.

  • Move the servo motor: To move the servo motor, use the PWM.ChangeDutyCycle() function, passing in the desired duty cycle as a parameter. This will correspond to the desired position of the servo motor. A duty cycle of 2.5% corresponds to the far left position of the motor, while a duty cycle of 12.5% corresponds to the far right position.

  • Clean up: After use, clean up the GPIO by using the GPIO.cleanup() function.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM) # set GPIO numbering mode to BCM
GPIO.setup(17, GPIO.OUT) # set GPIO 17 as an output
pwm = GPIO.PWM(17, 50) # create a PWM object with frequency of 50Hz
pwm.start(0) # start PWM with a duty cycle of 0 (0%)
time.sleep(1) # delay for 1 second

pwm.ChangeDutyCycle(7.5) # move the servo to the middle position (7.5%)
time.sleep(1)

pwm.ChangeDutyCycle(12.5) # move the servo to the far right position (12.5%)
time.sleep(1)

pwm.stop() # stop PWM signal
GPIO.cleanup() # clean GPIO

Programming Servo Motors using PWM Modulation

What is PWM?

Pulse Width Modulation is a technique used for controlling the amount of power supplied to a device by varying the pulse width of a periodic digital signal. In simple terms, it is a way of simulating an analog signal by turning a digital signal on and off at a specific frequency. The ratio of the on-time to the total time of a single pulse is known as the duty cycle and can be adjusted to control the output power.

PWM on Raspberry Pi:

The Raspberry Pi has a dedicated PWM peripheral that can generate up to 2 PWM signals simultaneously. It uses a 32-bit timer to generate the signals, allowing for accurate control of the output. The PWM peripheral on the Raspberry Pi also has advanced features like polarity inversion, which can be used to drive motors in both directions.

Utilizing PWM libraries and functions in Python:

Python is the most popular programming language for the Raspberry Pi, and it provides easy access to the PWM peripheral through various libraries. One of the main libraries for PWM on the Raspberry Pi is the RPi.GPIO library, which allows for the control of the GPIO pins on the Pi. This library has specific functions for setting up and controlling the PWM pins.

Here is an example of how to use the RPi.GPIO library to generate a PWM signal on GPIO pin 18 with a frequency of 1000Hz and a duty cycle of 50%:

```
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

pwm = GPIO.PWM(18, 1000)
pwm.start(50)

try:
while True:
pwm.ChangeDutyCycle(50) # set duty cycle to 50%
time.sleep(1) # wait for 1 second
pwm.ChangeDutyCycle(75) # set duty cycle to 75%
time.sleep(1) # wait for 1 second
except KeyboardInterrupt:
pwm.stop()
GPIO.cleanup()
```

Hands-on examples for programming servo motors using PWM modulation:

PWM signals are commonly used to control servo motors due to their simplicity and accuracy. Servo motors have a limited range of motion and can be precisely controlled by adjusting the duty cycle of the PWM signal. Here is an example of how to use PWM to control a servo motor with the Raspberry Pi using the RPi.GPIO library:

```
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

pwm = GPIO.PWM(18, 50) # set frequency to 50Hz
pwm.start(5) # set duty cycle to 5% (neutral position)

try:
while True:
pwm.ChangeDutyCycle(5) # set servo to neutral position
time.sleep(1)
pwm.ChangeDutyCycle(10) # rotate servo clockwise
time.sleep(1)
pwm.ChangeDutyCycle(2) # rotate servo counterclockwise
time.sleep(1)
except KeyboardInterrupt:
pwm.stop()
GPIO.cleanup()
```

No comments:

Post a Comment

Visual Programming: Empowering Innovation Through No-Code Development

In an increasingly digital world, the demand for rapid application development is higher than ever. Businesses are seeking ways to innovate ...