## Introduction: Hall Effect Sensors 3: PID Control

In this Instructable, we will be concluding our three part series on hall effect sensors by applying PID control to our position control. We will go over the three parts of PID control (Proportional, Integral, Derivative) and how they each affect the step response. This is Part 3 of the Hall Effect Sensor series. In the first week, we covered the basics of interrupts and hall effect sensors. In the second week, we covered how to synchronize the motion of multiple actuators by counting the hall effect pulses. Check out the previous Instructables via the links below:

**Part 1:**https://www.instructables.com/id/Hall-Effect-Sensors-1-Position-Control/

**Part 2:**https://www.instructables.com/id/Hall-Effect-Sensors-2-Synchronizing-Motion/

**For this Instructable, you will need:**

**- Actuator with Hall Effect Sensor**

**- MegaMoto motor control shield**

Before we get started, I would like to thank Brett Beauregard and his work on PID libraries for the Arduino. You can find a deeper explanation of the PID controls here. Now let's get started!

## Step 1: PID Control

Before we dive into programming PID control, we need to know what it is. PID stands for proportional integral derivative control. It is a control method used in closed feedback loops to get the results you want. A closed feedback loop is when the variable you are trying to change has a sensor that tells you the current value. Since you can see what the current value is you can easily make adjustments to reach the desired value. For example, the speedometer in a car tells you the value of what you are trying to control (speed). When you press the gas pedal, you think that you are going 30km/h. But the speedometer tells you that you are going 35km/h. You know that you need to ease off on the pedal to achieve your desired speed.

Now PID is a method of reaching your desired value in the desired way. There are lots of charts that show the difference in different feedback loops. But we will get to that later. For now, we need to understand how each part (P, I, and D) will affect your outcome. This is a very basic overview of what each part of the control system does. It is enough to get a general idea of how PID works, but it is barely scratching the surface. See here for more information.

The first variable is the Proportional variable. Going back to our car example, pretend that you start at 0km/h and want to reach 30km/h. The proportional part of your controller would tell the system to add 5km/h each second. This means it would take 6 seconds to reach your target speed. See the first image for a graph to explain better.

The second variable is the Integral variable. The integral part of the controller watches for small errors. Say that instead of increasing speed by 5km/h, you increase by 7km/h. After 5 seconds, you arrive at 35km/h. This is above your desired value but since you can only move in 7km/h steps, you can't decrease speed or else you will undershoot the target value. When the speed is maintained at 35km/h, the integral part of the controller notices that you are 5km/h off your target. After being wrong for X period of time, the controller says "hey, we need to adjust speed by 5km/h to reach our target". By doing this the integral part of the controller can monitor for accumulated errors and smooth the output to the desired value. See the second image for a graph to explain better.

The third variable is the Derivative variable. The derivative part of the controller allows you to reach your destination faster. The derivative variable is constantly adjusting how much it affects the output, depending how close you are to the desired value. Again, pretend you start at 0km/h. The derivative variable says "If we are between 0-10km/hr, speed will increase by 10km/h. If we are between 10-20km/h, the speed will increase by 7km/h. If we are between 20-30km/h, we are close to the desired speed, so the speed will increase by 5km/h." See the third image for a graph to explain better.

## Step 2: Hardware Setup

We need to wire the hall effect sensors the same way we did in Part 1. We are only going to use 1 hall effect signal per actuator.

**Wire them as follows:**

**- Red wire to 5V **

**- Black wire to GND **

**- Yellow wire to Arduino interrupt pin **

**- Actuator red wire to MOTA of the MegaMoto **

**- Actuator black wire to MOTB of the MegaMoto **

Check the beginning of the code in the next step, and ensure that the jumpers on the MegaMoto are set to the correct pins and that the hall effect sensors are set to the correct pins. Ensure that "hall0" and "hall1" correspond to the correct MegaMotos (PWMA0 and PWMA1, respectively).

**Once the motors are wired correctly to the boards, wire as follows:**

**- Connect 12V to BAT+**

**- Connect GND to BAT- **

**- Connect 12V to Vin on the Uno **

**- Wire two buttons between pins 4 and 7, connecting them to GND **

## Step 3: Programming PID

With the aide of Brett's Library, a PID algorithm can be created utilizing the detailed explanation of Sample Time, On/ Off, Tuning, and Computation. Brett's library progressively builds the PID code to it's final stage with adjustable fine tuning parameters, as well as proper documentation. This makes for proper understanding of each portion of a PID code.

Alternatively there is an Arduino PID Library which can be implemented in the code, but has only a brief explanation which may not provide proper understanding of the purpose and use of a PID algorithm.

## Step 4: Conclusion

In this Instructable, we learned how you can use PID control to get more precise control over a system. PID is the backbone of many different control systems and this Instructable was a small preview of what you can use it for.

If you'd like to take a look at our selection of linear actuators, motions control systems and microcontrollers then please visit us at www.progressiveautomations.com for all your actuator needs! We can even build a custom actuator or control system for you based on your own custom specifications with the help of our highly trained staff of engineers. You can learn more about the custom order process right here!

**Follow, Like and Subscribe! **

**Twitter** - www.twitter.com/ProgAutoInc

** Facebook** - www.facebook.com/ProgressiveAutomations

** Youtube** - www.youtube.com/user/MrActuators