Currently Malaysian are undergone into Recovery Movement Control Order phase and social distancing compliance is very important. There may be people who are not sure how far it is 1 meter. So, I developed a simple tag indicator to sense object distance in front
Hardware Requirements for this project:- Maker Nano
- Ultrasonic Sensor HC-SR04P (3.3V compatible)
- Donut board 9cm x 7 cm
- 4 pin Right-angle male header
- 40 pin Female header
- 2 pin Male header
- Wire cutter
- Solder station
- Thin wire *to make as wire jumper on the donut board
A little known about Maker Nano board, is an Arduino Nano compatible made by Cytron Technology where they're designed compact with built in leds, 2 push button and buzzer on the board itself. You may focused on the sensor and use less additional items thus make your project look less clutter. As you can see the pin out of the board below or you buy at here
Comparison between Arduino Nano and Maker Nano
Below is the comparison between Arduino Nano and Maker Nano:
Schematic
Since the Maker Nano already have built-in leds and buzzer, so the wiring schematic look less and clean as shown below where you can only focused on wiring the sensor and battery
Maker Nano HC-SR04P
- D2 Trigger Pin
- D3 Echo Pin
- GND GND Pin
- 3.3V VCC
Maker Nano 9VBattery
- VIN Positive Terminal
- GND Negative Terminal
final code will look like this.
//init pin for ultrasonic sensor
const int trigPin = 2; //D2 pin on maker nano
const int echoPin = 3 ; //D3 pin on maker nano
//init duration
long duration;
long now = millis();
long lastMeasure= 0;
//init distance in seconds
int distance= 0;
//init for buzzer pin
#define BUZZER 8
#define NOTE_E4 330
#define NOTE_G4 392
#define NOTE_B4 494
#define NOTE_D5 587
//init for led pin
#define Led4 4
#define Led5 5
#define Led6 6
#define Led7 7
#define Led8 8
#define Led9 9
#define Led10 10
#define Led11 11
#define Led12 12
int delayMs;
void setup() {
Serial.begin(115200);
//set pinmode for led
pinMode(Led4, OUTPUT);
pinMode(Led5, OUTPUT);
pinMode(Led6, OUTPUT);
pinMode(Led7, OUTPUT);
pinMode(Led8, OUTPUT);
pinMode(Led9, OUTPUT);
pinMode(Led10, OUTPUT);
pinMode(Led11, OUTPUT);
pinMode(Led12, OUTPUT);
//Set pimode for ultrasonic sensor
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
Serial.println("Start measure distance in centimeter.");
}
void loop() {
checkDistance();
//Print out to view in serial monitor
Serial.print("Distance: ");
Serial.println(distance);
//Check distance to turn on the led
// less than 10cm
if(distance < 10){
tenCM();
noteD5();
}
//less than 20cm
else if(distance < 20){
twentyCM();
noteD5();
}
//less than 30cm
else if(distance < 30){
thirtyCM();
noteB4();
}
//less than 40cm
else if(distance < 40){
fourtyCM();
noteB4();
}
//less than 50cm
else if(distance < 50){
fiftyCM();
noteG4();
}
//less than 60cm
else if(distance < 60){
sixtyCM();
noteE4();
}
//less than 70cm
else if(distance < 70){
seventyCM();
noteE4();
}
//less than 90cm
else if(distance < 90){
ninetyCM();
noteE4();
}
else{
outOfRange();
delayMs = 300;
}
delay(delayMs);
}
void checkDistance(){
//ultrasonic sensor measure distance
now = millis();
if(now - lastMeasure > 1000){
digitalWrite(trigPin, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
}
}
void noteD5(){
tone(BUZZER, NOTE_D5,50);
delay(50);
delayMs =30;
}
void noteB4(){
tone(BUZZER, NOTE_B4,50);
delay(50);
delayMs = 70;
}
void noteG4(){
tone(BUZZER, NOTE_G4,50);
delay(50);
delayMs = 110;
}
void noteE4(){
tone(BUZZER, NOTE_E4,70);
delay(50);
delayMs = 150;
}
void outOfRange(){
//Turn Off all leds
digitalWrite(Led4, LOW);
digitalWrite(Led5, LOW);
digitalWrite(Led6, LOW);
digitalWrite(Led7, LOW);
digitalWrite(Led8, LOW);
digitalWrite(Led9, LOW);
digitalWrite(Led10, LOW);
digitalWrite(Led11, LOW);
digitalWrite(Led12, LOW);
Serial.println("Out of distance");
}
void ninetyCM(){
//Turn Off Led except led on pin 12
digitalWrite(Led4, LOW);
digitalWrite(Led5, LOW);
digitalWrite(Led6, LOW);
digitalWrite(Led7, LOW);
digitalWrite(Led8, LOW);
digitalWrite(Led9, LOW);
digitalWrite(Led10, LOW);
digitalWrite(Led11, LOW);
digitalWrite(Led12, HIGH);
}
void seventyCM(){
//Turn Off Led except led on pin 12 & pin 11
digitalWrite(Led4, LOW);
digitalWrite(Led5, LOW);
digitalWrite(Led6, LOW);
digitalWrite(Led7, LOW);
digitalWrite(Led8, LOW);
digitalWrite(Led9, LOW);
digitalWrite(Led10, LOW);
digitalWrite(Led11, HIGH);
digitalWrite(Led12, HIGH);
}
void sixtyCM(){
digitalWrite(Led4, LOW);
digitalWrite(Led5, LOW);
digitalWrite(Led6, LOW);
digitalWrite(Led7, LOW);
digitalWrite(Led8, LOW);
digitalWrite(Led9, HIGH);
digitalWrite(Led10, HIGH);
digitalWrite(Led11, HIGH);
digitalWrite(Led12, HIGH);
}
void fiftyCM(){
digitalWrite(Led4, LOW);
digitalWrite(Led5, LOW);
digitalWrite(Led6, LOW);
digitalWrite(Led7, LOW);
digitalWrite(Led8, HIGH);
digitalWrite(Led9, HIGH);
digitalWrite(Led10, HIGH);
digitalWrite(Led11, HIGH);
digitalWrite(Led12, HIGH);
}
void fourtyCM(){
digitalWrite(Led4, LOW);
digitalWrite(Led5, LOW);
digitalWrite(Led6, LOW);
digitalWrite(Led7, HIGH);
digitalWrite(Led8, HIGH);
digitalWrite(Led9, HIGH);
digitalWrite(Led10, HIGH);
digitalWrite(Led11, HIGH);
digitalWrite(Led12, HIGH);
}
void thirtyCM(){
digitalWrite(Led4, LOW);
digitalWrite(Led5, LOW);
digitalWrite(Led6, HIGH);
digitalWrite(Led7, HIGH);
digitalWrite(Led8, HIGH);
digitalWrite(Led9, HIGH);
digitalWrite(Led10, HIGH);
digitalWrite(Led11, HIGH);
digitalWrite(Led12, HIGH);
}
void twentyCM(){
digitalWrite(Led4, LOW);
digitalWrite(Led5, HIGH);
digitalWrite(Led6, HIGH);
digitalWrite(Led7, HIGH);
digitalWrite(Led8, HIGH);
digitalWrite(Led9, HIGH);
digitalWrite(Led10, HIGH);
digitalWrite(Led11, HIGH);
digitalWrite(Led12, HIGH);
}
void tenCM(){
//turn on all leds
digitalWrite(Led4, HIGH);
digitalWrite(Led5, HIGH);
digitalWrite(Led6, HIGH);
digitalWrite(Led7, HIGH);
digitalWrite(Led8, HIGH);
digitalWrite(Led9, HIGH);
digitalWrite(Led10, HIGH);
digitalWrite(Led11, HIGH);
digitalWrite(Led12, HIGH);
}
Make your own TagSoldering Session
Modified Right Angle Female Header
If you don't have female right angle header, you can be a little bit creative you where you can combine Female Header with Male Right Angle Header to look like this
This is the final look of the board once finished soldering
You can power up maker nano using 9V battery or power bank
Next Upgrade- 3D print case
- Optimized code
Comments