top of page

Get Started with Arduino: Controlling a Servo and an Ultrasonic Sensor! 🤖


Greetings, young inventors! Today, you've stepped into Haruki's Robotics Lab, the heart and home of real makers and electronics enthusiasts. We're not just playing with gadgets here; we're shaping future innovations and igniting curiosity.


In this exciting journey, we're going to dive into the world of Arduino, a tiny computer that we can program to do amazing things. Ready to control a Servo (a type of motor) and an Ultrasonic Sensor (a device that can measure distance)? Gather your tools and let's start this adventure!



What you'll need 🧰


- An Arduino (Uno is a good one for beginners)

- A Servo motor

- An Ultrasonic sensor

- Some jumper wires (they're like tiny electric roads that let our signals travel around)


Part 1: Let's Control a Servo! 🦾


A servo is a small motor that can move to specific positions. You might have seen them in robots or even toy cars.


First, let's plug in our servo. Check your servo, it should have three wires:


- Brown or Black: Connects to GND on Arduino (it's like the ground where electricity ends its journey)

- Red: Connects to 5V on Arduino (it's like the food for our servo)

- Orange or Yellow: Connects to any digital pin, such as pin 9 (it tells the servo what to do)


Once you're done, we can tell the servo what to do with some code. Here's an easy program to make the servo move back and forth:



Explain:

#include <> in Arduino code includes a library necessary for programming.
The servo.attach(9) command in Arduino code connects the servo motor to digital pin 9 on the Arduino board.
The servo.write() command in Arduino code tells the servo motor to move to a specific angle, from 0 to 180 degrees.

 


Part 2: Let's Use an Ultrasonic Sensor! 🦇


An ultrasonic sensor is a cool device that can measure how far away things are, just like a bat!


First, let's plug in our sensor. It has four pins:


- VCC: Connects to 5V on Arduino (it's like the food for our sensor)

- GND: Connects to GND on Arduino (it's like the ground where electricity ends its journey)

- Trig: Connects to any digital pin, such as pin 9 (it sends out the sound waves)

- Echo: Connects to any digital pin, such as pin 10 (it listens for the sound waves coming back)


Once you're done, we can tell the sensor to measure distances. Here's a simple program to do it:



Explain:

The #define directive in Arduino assigns a name to a constant value.
Serial.begin() in Arduino code opens a channel for communication and sets the data rate in bits per second (baud) for serial data transmission.
Serial.print() is then used to send data from the Arduino board to a connected computer or other devices over this channel.
unsigned int in Arduino code refers to a non-negative integer variable type that can hold larger values than a regular "int" type.

Comments


bottom of page