Week 9: PAY HERE!

CONCEPT:

This week, I chose to work with an ultrasonic distance sensor to create a quirky barcode scanner. Since we still did not cover the sensor in class, I referred to SparkFun reference list and this video to know more about the sensor.

The LED lights light up and the piezo beeps once anything close (within a specific distance) is detected so that it mimicks the scanning of products’ barcodes. A switch button is also there in case the LEDs need to be switched manually (also to meet the requirements).  I handmade a funny setup of a cash register that you can check below.

COMPONENTS:

I used the following elements: BreadBoard/LEDs/Ultrasonic Sensor/Piezo Buzzer/Switch Button/Resistors/Wires/Arduino Uno, to make the following circuit:

The result was this funny looking cash register:

VIDEO:

Here is a video of how it works: cashier

CODE:
//pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int ledPin1 = 11;
const int buzzer = 3;
const int ledPin2 = 6;

//variables
long duration;
int distance;
bool buzzerOn = false;  

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  //to clear trigpin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);


  //set trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  //read echoPin & return the sound wave 
  duration = pulseIn(echoPin, HIGH);

  //calculating the distance
  distance = duration * 0.034 / 2;

  if (distance <= 50) {
    tone(buzzer, 2000); //frequency to make buzzer louder
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    buzzerOn = true;  //buzzer is on
  } else {
    if (buzzerOn) {
      noTone(buzzer); 
      buzzerOn = false;  //buzzer off
    }
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
//Prints the distance on the Serial Monitor
//Serial.print("Distance: ");
//Serial.println(distance);
  }
 
}

Basically, what’s happening here is that the code uses an ultrasonic sensor to measure distances based on the time it takes for a sound wave to bounce off an object and return. The trigger (trigPin) sends out a short ultrasonic pulse, and the echo (echoPin) detects the reflected pulse. The duration of the pulse’s travel time is then measured, and the distance is calculated based on the speed of sound. If the calculated distance is less than or equal to 50, the code activates the piezo buzzer at a frequency of 2000 and turns on two LEDs to indicate that an object is detected. The `buzzerOn` flag is used to prevent the buzzer from continuously sounding when an object remains within the detection range. When the distance exceeds 50 units, the code turns off the buzzer and the LEDs.

REFLECTION:

The code was the most challenging part in this assignment. I watched few tutorials (including the one referenced) to figure out how to work with the HC-SR04 and how to code the piezo buzzer part so that it is responsive to the distance. Also, I juggled many hardware issues including a short circuit costing me a sensor and almost my laptop plus a lot of damaged wires. Looking at the result now, I think it was worth taking the risk and working with something I never used before.

REFERENCES:

Sparkfun Kit: https://learn.sparkfun.com/resources?page=all

UltraSonic Sensor: https://youtu.be/ZejQOX69K5M?si=t1Vb3VMzbjPW2PnG

 

 

 

Leave a Reply