Here is all the documentation for assignment 5!
Schematic & Resistance Calculation Description: This schematic shows the circuit layout for assignment 5. The ultrasonic sensor is connected to the Arduino's PWM pins 11 and 12, and is supplied with 5V through the arduino. The LED strip is supplied by 12V of power through an external power supply. The current is controlled by a transistor which also uses 5V logic.
Circuit Description: This photo shows the physical implementation of the circuit on a breadboard. The aligator clips are used to connect the LED strip to the transistor and ground. The transistor is connected to the Arduino's PWM pin 9. The ultrasonic sensor is connected to the Arduino's pins 11 and 12. The LEDs are poweed by 12V of power through an external power supply.
/*
Ultrasonic Distance to LED Brightness
This code reads distance from an ultrasonic sensor
and turns the LED strip on/off depending on how close
an object or person is to the sensor.
I adapted the code from the Arduino IDE "Fade" example!
*/
// define which Arduino pins are connected to the ultrasonic sensor and strip (through transistor)
const int trigPin = 11; // pin that sends out the trigger signal to the ultrasonic sensor
const int echoPin = 12; // pin that receives the echo signal from the ultrasonic sensor
const int ledPin = 9; // PWM pin connected to LED strip through transistor
// declare variables for timing, distance, and brightness
long duration; // stores the time it takes for the sound wave to travel to the object and back
int distance; // stores the calculated distance (cm
int brightness; // stores the LED brightness value (0 = off, 255 = fully on)
void setup() {
pinMode(trigPin, OUTPUT); // set the trigger pin as an output
pinMode(echoPin, INPUT); // set the echo pin as an input
pinMode(ledPin, OUTPUT); // set the LED pin as an output so Arduino can control it
Serial.begin(9600); // start serial communication for debugging
}
void loop() {
// Send a short ultrasonic pulse as the first step to measure the distance of an object
digitalWrite(trigPin, LOW); // make sure trigger pin is low to start the signal
delayMicroseconds(2); // wait for 2 microseconds
digitalWrite(trigPin, HIGH); // set trigger pin high to send a sound wave
delayMicroseconds(10); // keep it high for 10 microseconds because this is the required pulse length for the ultrasonic sensor
digitalWrite(trigPin, LOW); // set trigger pin low again to stop sending the pulse
// Measure the time it takes for the echo to return
// Get the time, in microseconds, that the echo pin stays high
duration = pulseIn(echoPin, HIGH);
// Convert the time to distance
// sound travels at ~0.034 cm per microsecond, divide by 2 (because it is traveling there and back)
distance = duration * 0.034 / 2;
// Limit and map the distance to LED strip brightness
distance = constrain(distance, 0, 50); // limit the measured distance between 0 and 50 cm to avoid extreme values
brightness = map(distance, 50, 0, 0, 255); // map distance (0 - 50 cm) to brightness (0 - 255), such that 0cm = LEDs off, and 50cm = LEDs fully bright
// Apply brightness to the LED
// send PWM signal to LED pin to control the brightness
analogWrite(ledPin, brightness);
// Print distance and brightness values to Serial Monitor
Serial.print("Distance: ");
Serial.print(distance); // show measured distance in cm
Serial.print(" cm | Brightness: ");
Serial.println(brightness); // show current LED brightness value
// Adding a short delay before starting the loop again
delay(100);
}
Code Explanation: This Arduino program turns an LED strip on and off based on the distance of an object or person from an ultrasonic sensor. The distance is measured in centimeters and the brightness of the LEDs is controlled by the PWM signal sent to the transistor.
Operation Description: The GIF and video above demonstrate the actual operation of the circuit. As expected, the LED strip turns on when the ultrasonic sensor detects motion.
Answer: