Physical Computing and Robotics - Week 7-1
Physical Computing and Robotics - Week 7-1
(ITS310)
Week 7, Lecture 13
Ultrasonic Sensor, and Relays
Lecture Outline
➢ Exploring Ultrasonic Distance Sensor
➢ Exploring Relays
○ Controlling low power devices
○ Controlling high power devices
Modern Car Object Detection and Avoidance Parking Space - Occupied vs Available
Exploring Ultrasonic Distance Sensor
Ultrasonic Distance Sensor
● Examine this sketch:
#include <NewPing.h> // Download the library from manage library void loop() {
int cm = sonar.ping_cm(); //function to give
int TrigPin = 11; reading in cm
int EchoPin = 12; Serial.print("Distance in cm: ");
Serial.println(cm);
NewPing sonar(TrigPin, EchoPin, 100); //an object from the library delay(100);
//Three parameters: trig, echo, max distance [2 - 400]
}
void setup() {
Serial.begin(9600);
}
Ultrasonic Distance Sensor Challenge
You should be very careful with relays, as the high power may cause
severe damage to you and your electronic components.
● Relays are switches that can be controlled by very low power that can be obtained
from microcontroller outputs.
● They come in DC and AC output control with certain current load (amps).
Exploring Relays
Controlling High Power Devices (Example):
Exploring Relays
● Relay connections and schematics:
Exploring Relays
● Relay connections and schematics:
Exploring Relays
● Relays usually have four, five, or eight terminals depending on their type:
○ Single Pole Single Through (SPST) relays have only four terminals.
○ Single Pole Double Through (SPDT) relays have only five terminals.
○ Double Pole Double Through (DPDT) relays have eight terminals.
● The two COIL terminals connect to the low power provided by your microcontroller.
● The other terminals control how high power flows to your high power device(s).
○ For example, on a SPST relay, the other two terminals control the electricity
flow to your home appliances using the switch.
Exploring Relays
https://arduinogetstarted.com/tutorials/arduino-relay
Combining Ultrasonic with Relay
● Examine this sketch:
void loop() {
int cm = sonar.ping_cm();
#include <NewPing.h>
Serial.println(cm);
delay(200);
int TrigPin = 11;
int EchoPin = 12;
if(cm >= 10){
int RelayPin = 3;
digitalWrite(RelayPin, HIGH);
delay(50);
NewPing sonar(TrigPin, EchoPin, 100);
digitalWrite(RelayPin, LOW);
delay(50);
void setup() {
}
pinMode(RelayPin, OUTPUT);
else{
Serial.begin(9600);
digitalWrite(RelayPin, HIGH);
}
}
}
Challenge