Wednesday, 13 April 2022

Control Servo Motor with Raspberry Pi 4 using Python


Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4Welcome to the seventh tutorial of our Raspberry Pi programming course. An L293D IC driver and PWM were both used in the previous chapter to regulate the speed of a stepper motor. We looked at how to create a stepper motor’s circuit and conduct several steps. Nonetheless, in this lesson, we’ll look at how servo motors work and explore the various servo motors and their applications.

Components Used:

  • Raspberry Pi
  • Servo Motor
  • Male to female jumper wires
  • Personal computer

What is a servo motor?

There are unlimited possibilities in Robotics when you can use Raspberry Pi to interface with an Arduino-like servo motor and get it to do what you want.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

DC Motors, Gears, and Feed-Back Position Control Systems are all that is needed to create the basic yet powerful Servo Motor. A Servo Motor’s primary advantage is its ability to maintain its shaft’s angular position.

Servo Motors come in a variety of styles and sizes. Tower Pro SG90 and Tower Pro MG90S are two of the most popular Servo Motors.

In contrast to the metal gear motor, the plastic gear motor is known as the SG90. The SG90 Servo Motor will be used in this project.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

PWM (Orange or Yellow), VCC (Red), and GND are the three pins on the Tower Pro SG90 Servo Motor (Brown). Ensure the +5V and GND pins of the power supply are connected before proceeding.

Connecting the Servo Motor’s PWM or Signal Pin to the Controller’s PWM Output is required for proper operation (Raspberry Pi). In response to the PWM signal’s pulse width, the Servo Motor shaft’s angle changes.

Types of Servo Motors

There are three wires in a standard servo motor: one for power control, one for ground, and one for neutral. Their intended purpose determines the size and shape of these motors.

DC Servo Motors

DC servo motors usually have separate DC sources for the winding and armature windings. The armature current and the field current can be controlled to get the desired result. The advantages of field control over armature control are numerous. The advantages of armature control over field control are similar. The DC servo motor should be controlled in accordance with the applications. Since the DC servo motor’s armature inductive reactance is so low, it responds quickly and accurately to commands to start or stop. DC servo motors are used in similar components and computerized numerical control machinery.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

A/C Servo Motors

An AC servo motor, which incorporates an encoder, is employed for closed-loop control and feedback. This motor may be precisely positioned and regulated to meet the application’s needs. Better bearings and higher tolerances are common in these motors, and higher voltages are sometimes used in simple designs to boost torque. When it comes to robotics, CNC machines, and other automated systems, accuracy and adaptation are the key features of an AC motor.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Positional Rotation Servo Motor

It is the most common and is widely used in most applications. O/P rotation is approximately 180o. To keep the rotation sensor safe, it has physical brakes built into the gear mechanism. Many of these popular servos are used in radio-controlled water, radio-controlled automobiles, airplanes, robotics, toys, and many more applications.

Motors that can be used for continuous rotation

This motor varies from a more common positional rotation type, but it has an infinite rotation range. Rather than setting the servo’s fixed location, the control signal is interpreted as the servo’s rotation speed and direction. The servo may be made to revolve in either a clockwise or counterclockwise direction, at varying speeds, using various commands. If you’re riding a robot with a radar dish, you can utilize this type of motor for drive.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Motors with linear servos

Additional gears transform the output from circular motion to back-and-forth motion, like the positional rotation servo motor described above. Though hard to come by, servo motor actuators for higher-scale model airplanes can be found in hobby shops.
Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

How servo motors work

One of the three major components of a servomechanism is the motor, the feedback device, and the control electronics. It doesn’t matter if it’s brushed or brushless, rotatory, or even what shape it takes when it comes to a motor. The system can receive feedback from various sensors, such as potentiometers, encoders, linear transducers, and the like. This system’s servo capabilities are rounded out by an electronic control system that verifies feedback data and command references to make sure the servo motor is performing as taught. Multi-axis milling centers use brushless motors and motion control systems more complex than those used in recreational applications. There are many different ways to approach these concepts. Motor, sensor, and circuits are all standard components of a servo mechanism. This information is sent to the electronics via sensors, increasing the volume of gas flowing to the engine to maintain a predetermined speed.

The servo motor uses a permanent magnet DC motor with an inbuilt tachometer to calculate the output voltage. The drive electronics provide motors with electrical power in response to tachometer feedback voltages. After setting a commanded speed using a closed velocity loop, the driver’s circuitry compares the tachometer feedback voltage to the goal speed. The velocity loop monitors the tachometer feedback and the commanded velocity while the driver modulates the power in the motor.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

A sophisticated servo motor system has multiple integrated loops configured to optimal performance for the most precise motion control. For current, velocity, and location, the system uses feedback loops with high precision. To adjust parameters in real-time, each loop notifies the next and checks the feedback elements.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

The current loop, also known as the torque loop, is the foundation for all other loops. An electric motor’s acceleration or thrust is determined by the relationship between current and torque (or force, in the case of a linear motor). A current sensor is a device that measures the amount of current flowing through the motor and communicates this information to the user. Control electronics frequently receive proportional signals via analog or digital techniques. Ordered signals are subtracted from this signal. The servo motor needs to run at the specified current for an extended period to keep the loop intact. It will then update at sub-second intervals until it reaches the desired current.

Similarly, the velocity loop works with a voltage proportional to velocity in the same method. At low velocities, the current loop receives a command from the velocity loop to increase voltage.

The velocity loop is fed a velocity command from a PLC or motion controller that supplies the required current for acceleration and deceleration of the motor. The servo mechanism is controlled precisely and smoothly by the three loops working together in perfect harmony.

How to Control a Servo Motor using PWM?

Pulse Width Modulation is required to operate a servo motor. The pulse’s width or length controls the servo motor’s shaft position when using the PWM approach.

There is a defined frequency for the PWM signal, determined by the type of Servo Motor being used. The PWM Frequency of our SG90 and MG90S Servo Motors is 50Hz.

Pulse widths range from 1 millisecond for a 50Hz signal to 2 milliseconds for 20 milliseconds. In most cases, a servo motor’s sweep area is 180 degrees or 90 degrees on either neutral side.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

A pulse width of one millisecond (one-thousandth of a second) sets the servo’s position to the LEFT. This post has a duty cycle of 0.5 percent.

As with pulse widths of 1.5 and 2 microseconds, the servo is set to MIDDLE (7.5% duty cycle) and FAR RIGHT, respectively (with a duty cycle of 10 percent).

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Circuit Diagram of Raspberry Pi Servo Motor Interface

A single PWM Pulse pin is available on the Raspberry Pi. Pin 12 on a computer is what I’m referring to. Here is a diagram of the Raspberry Pi Servo Motor Interface’s circuit.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Here’s a wiring diagram that can help you visualize the connections better:

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Circuit Design

The motor has three wires: red, brown, and orange, which you should pay attention to. The color red denotes positivity, while the color brown denotes negativity.

Pulse-width modulation control signals are sent through the orange wires. Three separate wires connect Pins on the raspberry pi.

For this reason, we will use jumper cables as depicted in the image below:

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

A 5-volt power supply is connected to Pin 4 (signal wire), while the ground is connected to Pin 6 (ground).

Code

Servo Motor Control using Raspberry Pi is implemented in Python. We’ll be using our favorite code editor, thorny, so get it up and running on your Raspberry Pi.

Importing libraries

Importing the necessary libraries is the first step. Time and the GPIO of the Raspberry pi in this scenario.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

GPIO numbering mode can be set like this.

When the GPIO numbering mode is changed here, it will be consistent with the board’s numbering system.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Configuring pin 11 as the output and using PWM to control the servo

We then set pin 11 to be the output.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

After that, we’ll create a variable. I used PWM to name my servo’s pin 11. 50 is the pulse frequency at pin 11, while 11 is just the PIN.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Begin running PWM, except with pulse off

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Rotate the Servo shaft

Variable duties are defined and assigned random values, such as 2. When counting our intervals, it serves as the starting point.

After that, we’ll run a for loop to find all the duty values between 2 and 17. Given the range of 2 to 17 as an interval, The motor will move the shaft if duty is increased by one every time the shaft moves. After every one-second interval, the shaft rotates 12 degrees to the right until it reaches 180 degrees.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Let us turn the shaft back.

Changing the duty cycle to perform a 180-degree turn back to 0 degrees is all it takes to reset the shaft to 0 degrees.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Clean up

The following code snippet is used to clean the pins.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

When all of the code in the example above is run, the motor shaft will be rotated 180 degrees back and forth. Cleanup will then begin.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

You can experiment with multiple motors to see how much fun this can be. This project’s complete source code can be seen below.

Control Servo Motor with Raspberry Pi 4 using Python, control servo motor with RPi4, servo motor Pi4, servo motor raspberry pi 4, raspberry pi 4 servo motor, pi 4 servo, servo pi4

Working on the setup

When it comes time to put the concept into action, we’ll take advantage of the Raspberry Pi’s PWM functionality. If you’ve been following along, you know that the Servo Motor position changes depending on the PWM signal from the Raspberry Pi.

To achieve a sweeping effect from the Servo Motor, we need to alter the PWM signal’s Duty Cycle between 5% and 10%, corresponding to the extreme left and right positions.

If you look at the code, you’ll see that the duty cycle steadily increases from 5% to 10%, with a 0.5 percent increase at each stage. The reversal will commence as soon as it hits 10%.

Applications of servo motors

Many applications can benefit from regulating the angle of rotation of the Servo Motor via the Raspberry Pi, such as:

Cameras, telescopes, and antennas

It is impossible to find a radio signal, take pictures of a galaxy billions of light-years away, or photograph a live subject without using a servo motor. One of the advantages of using servo motors is that they can be used in various applications.

Elevator

It’s all about safety while designing and implementing transportation networks for buildings. Servo motors are extensively used in elevators in some of the world’s highest buildings to carry passengers safely and smoothly.

Robotics

Robotics is a hot topic, and their practical applications are growing at an ever-increasing rate. Servo motors, which are compact, powerful, and precise due to their changing size and force density, are the most widely utilized in robotics. Using robots to control the detonation of bombs, autonomous firefighter trucks, or even the joints in robot arms is all possible.

Industrial production

To eliminate the risk of human error and speed up production, manufacturers are creating robotic alternatives. Another robot example is the pick and place robot, which can move items from one side of an industrial building to the other. Servo motors are commonly utilized when mobility or rotations could be hazardous.

Precision and power are critical features of servo motors, which are used in bending and cutting metal sheets and high-speed milling machines. Servo motors are commonly employed in the spinning sections of conveyor systems in various industries, such as the food and beverage industry.

Conclusion

Congratulations! You have made it to the end of this tutorial. We have seen how PWM is used to control a servo motor. A variety of servo motor designs and real-world applications have also been demonstrated and their application in real life. The following tutorial will teach how to use Python to interface an Ultrasonic Sensor to a Raspberry Pi 4.

The post Control Servo Motor with Raspberry Pi 4 using Python appeared first on The Engineering Projects.



No comments:

Post a Comment