In this tutorial, we are going to see how to interface the servo motor to Arduino Uno and control it using an IR sensor using PictoBlox, our graphical programming software. A servo motor, or simply, a servo, is a rotary actuator that rotates or push parts of a machine to which it is connected, with precision. They rotate in a particular angle and then stop.
You can download PictoBlox from here.
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Hardware Components Required
- Arduino Uno (Note: You can use any other Arduino Board of your choice or ESP32)
- Micro Servo Motor with Accessories
- IR Sensor
- USB A-B Cable
- Male-to-Male Jumper Cable
- Male-to-Female Jumper Cable
Step 2: What Is an Actuator?
An actuator is a device that actuates something. In simple words, it produces motion. It helps other devices move and interact with their surrounding environment. The actuator takes in energy and uses it to help the device move. Servo motors and DC motors are types of electric actuators.
Step 3: Connecting the Servo Motor and IR Sensor to UNO
Let's begin by connecting our Servo Motor and IR Sensor to Arduino Uno.
Note: You can use any board of your choice, all you need to do is change the hat block.
First, connect the servo to Uno.
- Connect the servo’s brown wire to Uno’s GND pin
- Connect the red wire to Uno’s VIN pin
- Connect the orange wire to Uno’s PWM pin 5
Now, let’s connect the IR sensor to Uno.
- Connect the sensor’s VCC pin to Uno’s 5V pin.
- Connect the sensor’s GND pin to Uno’s GND pin.
- Connect the sensor’s OUT pin to Uno’s Digital pin 2.
Step 4: Interfacing Arduino Uno to PictoBlox
Step 5: Controlling Servo Using IR Sensor
We are going to make servo turn 45° if the IR sensor detects an object, otherwise, it should remain at 0°.
Let's write the script in PictoBlox for the same:
To check whether the IR Sensor has detected an object or not, we'll use the read digital sensor block.
Use an if-else and drag and drop it into the hexagonal space of the if-arm.
Now, we need to make the servo turn 45° if the if condition is true. Thus, use the set servo block from the Actuators palette.
Write 45 in the white space given and place it in below the if arm.
Now, the servo shouldn't turn if any object is not detected. use the set servo block and write 0 in the white space.
Finally, add a forever block along with the hat block of when Arduino Uno starts up above the entire script.
Switch to Upload mode by toggling this button. Click on the Upload Code button to upload the code
Step 6: Final Output
Try bringing the hand close to the IR sensor the servo rotates to 45° and when you take your hand away it turns back to 0°.
Step 7: Taking the Activity a One Step Closer
We have made a miniature house whose door opens automatically as soon as the IR sensor detects an object. And closes as soon as the IR sensor stops detecting the object. We will use the servo to open and close the door. Try making the same for you.
Explore: Try interfacing the DC motor to Uno and control it using the Sensor.