Components and supplies
Arduino UNO
5 mm LED: Red
5 mm LED: Green
Through Hole Resistor, 560 ohm
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
Ultrasonic counter
arduino
Code for the ultrasonic occupancy counter. Set the 'limit' to the maximum number of people allowed in the shop.
1/* 2 This project was developed by the Design and Manufacturing Futures Lab at the University of Bristol as part of Project Clean Access 3 More information about the lab and the project can be found here: https://dmf-lab.co.uk/project-clean-access/ 4 This code is for a counter to be placed in a shop entrance with separate entry and exit channels 5 There are detailed instructions for this project on the Instructables website: 6 7 The hardware required for this product: 8 2 x HC-SR04 Ultrasonic sensor 9 2 x 560 Ohm resistor 10 1 x Red LED 11 1 x Green LED 12 1 x Arduino Uno 13 Power supply 14 Correct USB cable for uploading sketch to Arduino board 15 16 Credit goes to Tim Eckel for developing the NewPing library and example sketches. 17*/ 18 19#include <NewPing.h> 20//Defining where the components are attached 21#define TRIG_IN A1 22#define TRIG_OUT 3 23#define ECHO_IN A2 24#define ECHO_OUT 4 25#define LED_WAIT 12 26#define LED_ENTER 9 27 28#define iterations 5 //Number of readings in the calibration stage 29#define MAX_DISTANCE 150 // Maximum distance (in cm) for the sensors to try to read. 30#define DEFAULT_DISTANCE 45 // Default distance (in cm) is only used if calibration fails. 31#define MIN_DISTANCE 15 // Minimum distance (in cm) for calibrated threshold. 32 33float calibrate_in = 0, calibrate_out = 0; // The calibration in the setup() function will set these to appropriate values. 34float distance_in, distance_out; // These are the distances (in cm) that each of the Ultrasonic sensors read. 35int count = 0, limit = 5; //Occupancy limit should be set here: e.g. for maximum 8 people in the shop set 'limit = 8'. 36bool prev_inblocked = false, prev_outblocked = false; //These booleans record whether the entry/exit was blocked on the previous reading of the sensor. 37 38NewPing sonar[2] = { // Sensor object array. 39 NewPing(TRIG_IN, ECHO_IN, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. 40 NewPing(TRIG_OUT, ECHO_OUT, MAX_DISTANCE) 41}; 42 43/* 44 A quick note that the sonar.ping_cm() function returns 0 (cm) if the object is out of range / nothing is detected. 45 We will include a test to remove these erroneous zero readings later. 46*/ 47 48void setup() { 49 Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. 50 pinMode(2, OUTPUT); pinMode(5, OUTPUT); pinMode(A0, OUTPUT); pinMode(A3, OUTPUT); pinMode(11, OUTPUT); 51 digitalWrite(2, HIGH); digitalWrite(5, LOW); digitalWrite(A0, HIGH); digitalWrite(A3, LOW); digitalWrite(11, LOW); 52 pinMode(LED_WAIT, OUTPUT), pinMode(LED_ENTER, OUTPUT); 53 digitalWrite(LED_WAIT, HIGH); digitalWrite(LED_ENTER, HIGH); //Both LEDs are lit to alert user to ongoing calibration. 54 Serial.println("Calibrating..."); 55 delay(1500); 56 for (int a = 0; a < iterations; a++) { 57 delay(50); 58 calibrate_in += sonar[0].ping_cm(); 59 delay(50); 60 calibrate_out += sonar[1].ping_cm(); 61 delay(200); 62 } 63 calibrate_in = 0.75 * calibrate_in / iterations; //The threshold is set at 75% of the average of these readings. This should prevent the system counting people if it is knocked. 64 calibrate_out = 0.75 * calibrate_out / iterations; 65 66 if (calibrate_in > MAX_DISTANCE || calibrate_in < MIN_DISTANCE) { //If the calibration gave a reading outside of sensible bounds, then the default is used 67 calibrate_in = DEFAULT_DISTANCE; 68 } 69 if (calibrate_out > MAX_DISTANCE || calibrate_out < MIN_DISTANCE) { 70 calibrate_out = DEFAULT_DISTANCE; 71 } 72 73 Serial.print("Entry threshold set to: "); 74 Serial.println(calibrate_in); 75 Serial.print("Exit threshold set to: "); 76 Serial.println(calibrate_out); 77 digitalWrite(LED_WAIT, LOW); digitalWrite(LED_ENTER, LOW); //Both LEDs are off to alert user that calibration has finished. 78 delay(1000); 79} 80 81void loop() { 82 // Serial.print("Count: "); 83 // Serial.println(count); 84 distance_in = sonar[0].ping_cm(); 85 delay(40); // Wait 40 milliseconds between pings. 29ms should be the shortest delay between pings. 86 distance_out = sonar[1].ping_cm(); 87 delay(40); 88 if (distance_in < calibrate_in && distance_in > 0) { // If closer than wall/calibrated object (person is present) && throw out zero readings 89 if (prev_inblocked == false) { 90 count++; // Increase count by one 91 Serial.print("Count: "); 92 Serial.println(count); 93 } 94 prev_inblocked = true; 95 } else { 96 prev_inblocked = false; 97 } 98 if (distance_out < calibrate_out && distance_out > 0) { 99 if (prev_outblocked == false) { 100 count--; // Decrease count by one 101 Serial.print("Count: "); 102 Serial.println(count); 103 } 104 prev_outblocked = true; 105 } else { 106 prev_outblocked = false; 107 } 108// //If there are fewer people in the shop than the limit, light is green, else it is red 109 if (count < limit) { 110 digitalWrite(LED_WAIT, LOW); 111 digitalWrite(LED_ENTER, HIGH); 112 } else { 113 digitalWrite(LED_WAIT, HIGH); 114 digitalWrite(LED_ENTER, LOW); 115 } 116}
Ultrasonic counter
arduino
Code for the ultrasonic occupancy counter. Set the 'limit' to the maximum number of people allowed in the shop.
1/* 2 This project was developed by the Design and Manufacturing Futures Lab at the University of Bristol as part of Project Clean Access 3 More information about the lab and the project can be found here: https://dmf-lab.co.uk/project-clean-access/ 4 This code is for a counter to be placed in a shop entrance with separate entry and exit channels 5 There are detailed instructions for this project on the Instructables website: 6 7 The hardware required for this product: 8 2 x HC-SR04 Ultrasonic sensor 9 2 x 560 Ohm resistor 10 1 x Red LED 11 1 x Green LED 12 1 x Arduino Uno 13 Power supply 14 Correct USB cable for uploading sketch to Arduino board 15 16 Credit goes to Tim Eckel for developing the NewPing library and example sketches. 17*/ 18 19#include <NewPing.h> 20//Defining where the components are attached 21#define TRIG_IN A1 22#define TRIG_OUT 3 23#define ECHO_IN A2 24#define ECHO_OUT 4 25#define LED_WAIT 12 26#define LED_ENTER 9 27 28#define iterations 5 //Number of readings in the calibration stage 29#define MAX_DISTANCE 150 // Maximum distance (in cm) for the sensors to try to read. 30#define DEFAULT_DISTANCE 45 // Default distance (in cm) is only used if calibration fails. 31#define MIN_DISTANCE 15 // Minimum distance (in cm) for calibrated threshold. 32 33float calibrate_in = 0, calibrate_out = 0; // The calibration in the setup() function will set these to appropriate values. 34float distance_in, distance_out; // These are the distances (in cm) that each of the Ultrasonic sensors read. 35int count = 0, limit = 5; //Occupancy limit should be set here: e.g. for maximum 8 people in the shop set 'limit = 8'. 36bool prev_inblocked = false, prev_outblocked = false; //These booleans record whether the entry/exit was blocked on the previous reading of the sensor. 37 38NewPing sonar[2] = { // Sensor object array. 39 NewPing(TRIG_IN, ECHO_IN, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. 40 NewPing(TRIG_OUT, ECHO_OUT, MAX_DISTANCE) 41}; 42 43/* 44 A quick note that the sonar.ping_cm() function returns 0 (cm) if the object is out of range / nothing is detected. 45 We will include a test to remove these erroneous zero readings later. 46*/ 47 48void setup() { 49 Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. 50 pinMode(2, OUTPUT); pinMode(5, OUTPUT); pinMode(A0, OUTPUT); pinMode(A3, OUTPUT); pinMode(11, OUTPUT); 51 digitalWrite(2, HIGH); digitalWrite(5, LOW); digitalWrite(A0, HIGH); digitalWrite(A3, LOW); digitalWrite(11, LOW); 52 pinMode(LED_WAIT, OUTPUT), pinMode(LED_ENTER, OUTPUT); 53 digitalWrite(LED_WAIT, HIGH); digitalWrite(LED_ENTER, HIGH); //Both LEDs are lit to alert user to ongoing calibration. 54 Serial.println("Calibrating..."); 55 delay(1500); 56 for (int a = 0; a < iterations; a++) { 57 delay(50); 58 calibrate_in += sonar[0].ping_cm(); 59 delay(50); 60 calibrate_out += sonar[1].ping_cm(); 61 delay(200); 62 } 63 calibrate_in = 0.75 * calibrate_in / iterations; //The threshold is set at 75% of the average of these readings. This should prevent the system counting people if it is knocked. 64 calibrate_out = 0.75 * calibrate_out / iterations; 65 66 if (calibrate_in > MAX_DISTANCE || calibrate_in < MIN_DISTANCE) { //If the calibration gave a reading outside of sensible bounds, then the default is used 67 calibrate_in = DEFAULT_DISTANCE; 68 } 69 if (calibrate_out > MAX_DISTANCE || calibrate_out < MIN_DISTANCE) { 70 calibrate_out = DEFAULT_DISTANCE; 71 } 72 73 Serial.print("Entry threshold set to: "); 74 Serial.println(calibrate_in); 75 Serial.print("Exit threshold set to: "); 76 Serial.println(calibrate_out); 77 digitalWrite(LED_WAIT, LOW); digitalWrite(LED_ENTER, LOW); //Both LEDs are off to alert user that calibration has finished. 78 delay(1000); 79} 80 81void loop() { 82 // Serial.print("Count: "); 83 // Serial.println(count); 84 distance_in = sonar[0].ping_cm(); 85 delay(40); // Wait 40 milliseconds between pings. 29ms should be the shortest delay between pings. 86 distance_out = sonar[1].ping_cm(); 87 delay(40); 88 if (distance_in < calibrate_in && distance_in > 0) { // If closer than wall/calibrated object (person is present) && throw out zero readings 89 if (prev_inblocked == false) { 90 count++; // Increase count by one 91 Serial.print("Count: "); 92 Serial.println(count); 93 } 94 prev_inblocked = true; 95 } else { 96 prev_inblocked = false; 97 } 98 if (distance_out < calibrate_out && distance_out > 0) { 99 if (prev_outblocked == false) { 100 count--; // Decrease count by one 101 Serial.print("Count: "); 102 Serial.println(count); 103 } 104 prev_outblocked = true; 105 } else { 106 prev_outblocked = false; 107 } 108// //If there are fewer people in the shop than the limit, light is green, else it is red 109 if (count < limit) { 110 digitalWrite(LED_WAIT, LOW); 111 digitalWrite(LED_ENTER, HIGH); 112 } else { 113 digitalWrite(LED_WAIT, HIGH); 114 digitalWrite(LED_ENTER, LOW); 115 } 116}
Downloadable files
Circuit diagram
All components attach directly to the Arduino Uno
Circuit diagram
Documentation
Case top
To be 3D printed, this holds all the components and presents the project more cleanly.
Case top
Case bottom
To be 3D printed, this holds all the components and presents the project more cleanly.
Case bottom
Case top
To be 3D printed, this holds all the components and presents the project more cleanly.
Case top
Case bottom
To be 3D printed, this holds all the components and presents the project more cleanly.
Case bottom
Comments
Only logged in users can leave comments