SensorsParking Sensor with Sound and Lights
An ultrasonic sensor measures the distance. Three LEDs show how close the obstacle is, and the buzzer beeps faster the closer you get.
Home and IoTType the secret code and press #: with the right code the green light comes on and a servo slides the bolt open, with the wrong one the buzzer beeps three times.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
Find and install “Keypad” (Mark Stanley, Alexander Brevig) in the Library Manager.
The keypad has eight pins. The first four from the left are the rows: connect them to pins 9, 8, 7 and 6. The other four are the columns: pins 5, 4, 3 and 2.
Connect the servo's orange wire to pin 10, red to 5V and brown to ground. The green LED goes to A0 and the red one to A1 (both through 330 Ω resistors), and the buzzer's “+” to A2. The LEDs' short legs and the buzzer's “−” go to ground. The analogue pins work as ordinary digital pins too.
The secret code is on the CODE line, 1234 to begin with. * clears what you typed, # checks it. The part starting with #ifdef is for the Uno R4, where the Keypad library can't switch on the internal resistors by itself.
#include <Keypad.h>
#include <Servo.h>
const byte ROWS = 4, COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
#ifdef ARDUINO_API_VERSION // Uno R4: switch the keypad's pull-ups on the R4 way
#undef pinMode
class KeypadR4 : public Keypad {
public:
using Keypad::Keypad;
void pin_mode(byte pin, byte mode) override { pinMode(pin, (PinMode)mode); }
};
KeypadR4 keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
#else
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
#endif
const String CODE = "1234"; // your secret code
const int SERVO = 10, GREEN = A0, RED = A1, BUZZER = A2;
const int LOCKED = 0, UNLOCKED = 90; // bolt angles
Servo bolt;
String typed = "";
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
bolt.attach(SERVO);
bolt.write(LOCKED);
digitalWrite(RED, HIGH);
}
void loop() {
char key = keypad.getKey();
if (!key) return;
tone(BUZZER, 2000, 30); // a click for every key
if (key == '*') { typed = ""; return; } // * clears
if (key != '#') { typed += key; return; } // # checks the code
if (typed == CODE) {
digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
bolt.write(UNLOCKED);
tone(BUZZER, 2500, 300);
delay(5000); // open for 5 seconds
bolt.write(LOCKED);
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
} else {
for (int i = 0; i < 3; i++) { tone(BUZZER, 400, 150); delay(250); }
}
typed = "";
}Attach a small bolt to the servo arm: at 0 degrees it locks the door, at 90 it lets go. If it works the other way round on your box, swap the LOCKED and UNLOCKED numbers.
SensorsAn ultrasonic sensor measures the distance. Three LEDs show how close the obstacle is, and the buzzer beeps faster the closer you get.
Home and IoTA PIR sensor notices a warm body moving. As soon as someone walks into the room, a siren sounds and a red light flashes.
Home and IoTBring your hand close and the lid lifts by itself. An ultrasonic sensor notices the hand, and a servo opens the lid and closes it three seconds after the hand has gone.