I began a Robotics Membership just lately on the Institute I’m presently working at wherein I’ve been slowly getting my newbie college students to develop into acquianted with Arduino-based robots and Arduino coding.
We bought a set of Lafvin 2WD Robotic Kits (see picture above) from Little Fowl Electronics and step-by-step I’ve been getting my college students to discover ways to management every bit of {hardware} connected to the robotic. We’re working in direction of getting the robotic to maneuver round a room with out bumping into something it would encounter alongside the way in which.
On this submit I believed I’d share some code I’ve written for this membership that makes use of an ultrasonic sensor mounted on a servo motor to detect the space of obstacles from the robotic.
The code will get the rotor to brush 180 levels in a single route after which again once more. It stops, nevertheless, each 45 levels to make use of the ultrasonic sensor to detect if there may be an object inside 30cm (1 foot) of the machine. If there may be, a message is printed on the Serial Monitor.
Key tools used is as follows:
- Arduino Uno R3
- Arduino Sensor Growth Protect V5.0 (connected to Uno)
- SG90 Micro Servo motor
- Ultrasonic Sensor (HC-SR04)
This video reveals how we’ve got put collectively the items and connections. The related connection diagrams are as follows:


The code under is well-commented so there’s no want for me to elucidate any extra. However please notice that you’ll want to put in the “Servo” library earlier than operating this code.
When you have any questions, submit them under and I’ll get again to you as quickly as I can.
/*
This code makes use of an Ultrasonic (US) sensor mounted on a servo motor to detect obstacles
for a transferring robotic. The rotor sweeps 180 levels from left to proper after which
again once more. It takes readings each 45 levels (and 200 milliseconds). If there may be
an object inside 30cm, it prints a message to the Serial monitor.
As soon as the impediment is cleared, the rotor will begin sweeping once more.
My code works for an Arduino Uno R3 board and a Sensor ExapansionShield v5.0.
The rotor and sensor are linked to pins A0-A2 of the Energy Protect.
I've additionally put in skeleton code (by way of "sweep" variable) to point the place one may
write code to manoeuvre a robotic if an impediment is encountered.
*/
#embrace <Servo.h> // Servo library
#embrace "SR04.h" // Ultrasonic (US) sensor library
#outline TRIG_PIN A1 // UltraSonic I/O
#outline ECHO_PIN A0 // UltraSonic I/O
// Create Ultrasonic object
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
int obj_distance; // variable to retailer distance returned by US sensor
int distance_threshold = 30; // how near get to an object earlier than deciding to behave
// Create a servo object
Servo myServo;
int sweep_directions[] = {0, 45, 90, 135, 180}; // attainable rotor positions (levels)
int direction_index = 0; // present index of rotor place
int route = 1; // Course of rotor: 1 for ahead (sweep proper), -1 for backward (sweep left)
// Time monitoring variables to solely transfer the rotor between sure elapsed time intervals
unsigned lengthy previousMillis = 0;
const unsigned int interval = 200; // interval in milliseconds between US readings
// this boolean just isn't used on this code however I left it right here to present you an concept of how
// you might construct an impediment avoiding robotic sooner or later
bool sweep = true;
void setup() {
// Begin the serial monitor
Serial.start(9600);
// Join servo motor to protect
myServo.connect(A2);
}
void loop() {
// Get the present time
unsigned lengthy currentMillis = millis();
// Examine if the interval has handed from final studying
if (currentMillis - previousMillis >= interval) {
obj_distance = sr04.Distance(); // get impediment distance
if(obj_distance <= distance_threshold) {
// impediment detected inside distance threshold
Serial.print("STOP! Impediment distance: ");
Serial.println(obj_distance);
/*
You'd write code right here to cease the robotic from transferring,
set sweep to false to present the robotic time to work out
which route to show after which to manoeuvre.
As soon as, we're good to go once more, set sweep to true.
*/
}
else if(sweep) {
// If we're in "sweep" state, we modify rotor route and
// wait one other 200 milliseconds from the final studying
myServo.write(sweep_directions[direction_index]);
Serial.print("Sweep route:");
Serial.println(sweep_directions[direction_index]);
// Examine if the tip or starting of the array is reached
if (direction_index == 4) {
route = -1; // Reverse route of sweeping on the finish
} else if (direction_index == 0) {
route = 1; // Reverse route initially
}
direction_index += route; // increment or decrement index accordingly
}
previousMillis = currentMillis;
}
}
To learn when new content material like that is posted, subscribe to the mailing listing:
(Be aware: If this submit is discovered on a web site apart from zbigatron.com, a bot has stolen it – it’s been taking place quite a bit currently)


