Circuit Documentation
Summary
This circuit involves an Arduino 101 microcontroller, an HC-SR04 Ultrasonic Sensor, and an MG996R Servo Motor. The ultrasonic sensor measures distance and triggers the servo motor to move when an object is detected within a specified range. The Arduino 101 controls the entire operation, reading the sensor data and driving the servo motor based on the distance measured.
Component List
Arduino 101
- Description: A microcontroller board based on the Intel Curie module.
- Pins: A5/SCL, A4/SDA, AREF, GND, D13/SCK, D12/MISO, D11 PWM/MOSI, D10 PWM/SS, D9 PWM, D8, D7, D6 PWM, D5 PWM, D4, D3 PWM, D2, D1/TX, D0/RX, AIN, ioref, RESET, 3V3, 5V, VIN, A0, A1, A2, A3, ICSP MISO, ICSP SCK, ICSP MOSI
HC-SR04 Ultrasonic Sensor
- Description: A sensor used to measure distance by using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
MG996R Servo Motor
- Description: A high-torque servo motor used for precise control of angular position.
- Pins: GND, VCC, SIG
Qwiic Cable - Breadboard Jumper (4-pin)
- Description: A cable used for connecting Qwiic-enabled devices to a breadboard.
- Pins: GND, 3.3V, SDA, SCL
Wiring Details
Arduino 101
- 5V: Connected to VCC of HC-SR04 Ultrasonic Sensor and VCC of MG996R Servo Motor
- GND: Connected to GND of HC-SR04 Ultrasonic Sensor and GND of MG996R Servo Motor
- D10 PWM/SS: Connected to ECHO of HC-SR04 Ultrasonic Sensor
- D9 PWM: Connected to TRIG of HC-SR04 Ultrasonic Sensor
- D3 PWM: Connected to SIG of MG996R Servo Motor
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V of Arduino 101
- GND: Connected to GND of Arduino 101
- TRIG: Connected to D9 PWM of Arduino 101
- ECHO: Connected to D10 PWM/SS of Arduino 101
MG996R Servo Motor
- VCC: Connected to 5V of Arduino 101
- GND: Connected to GND of Arduino 101
- SIG: Connected to D3 PWM of Arduino 101
Code Documentation
Arduino 101 Code
#include <Servo.h>
const int trigPin = 9;
const int echoPin = 10;
const int servoPin = 3;
const int distanceThreshold = 10; // Distance in cm to trigger the servo
Servo myServo;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myServo.attach(servoPin);
myServo.write(0); // Initial position of the servo
Serial.begin(9600);
}
void loop() {
long duration, distance;
// Send a 10us pulse to trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pin
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = (duration / 2) / 29.1;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// If the distance is less than the threshold, activate the servo
if (distance < distanceThreshold) {
myServo.write(90); // Move the servo to 90 degrees
delay(1000); // Wait for 1 second
myServo.write(0); // Move the servo back to 0 degrees
}
delay(500); // Wait for 0.5 seconds before the next loop
}
This code initializes the pins for the ultrasonic sensor and the servo motor. It continuously measures the distance using the ultrasonic sensor and moves the servo motor to 90 degrees if an object is detected within the specified threshold distance. The servo motor then returns to its initial position after a short delay. The distance measurements are also printed to the Serial Monitor for debugging purposes.
