Search

How to use HC-SR04 Ultrasonic Sensor with Cricket

Updated: Apr 8

Foreword: it is our customer project from Khairul Alam who kindly shares how he is making IoT parking lot detector by using an ultrasonic sensor, Arduino and IoT Cricket Wi-Fi module. He also provides suggestions on how to make it in a smaller form factor and power directly on batteries.


In this project I'm going to show how to use HC-SR04 to measure a distance and send a notification to a smartphone when an object is detected within 30cm range. Here I use Arduino UNO (but it can be any other micro-controller) to evaluate a distance from the ultrasonic sensor. I also use IOT Cricket to connect to the internet over WiFi network and send notifications on a phone. I'm going to configure Cricket to use the Pushsafer service to send push notifications but of course you can use alternative services like Pushover, IFTTT, Blynk, Zapier, Emails, Firebase or any other internet service you like.


You can modify this project to make a smart vehicle detector for a parking lot and you can get notified to your phone on the absence or presence of a car. Installing a device on every slot you can also count total number of free slots available in a parking lot and visualise the information from smartphone application, web dashboard or integrate to the system by using MQTT / HTTP RESTfulAPI.


So, without further discussing let's start making!


Prerequisites



Step 1: Connecting HC-SR04 ultrasonic sensor to Arduino

The ultrasonic sensor uses sonar (ultrasound) to determine distance of an object. It is a non-contact sensor for determining distance or range with high accuracy and stability. An ultrasonic sensor consists of an ultrasonic transmitter and a receiver. The transmitter sends 8 cycle burst of ultrasound at 40KHz and the receiver waits to receive an echo. The distance is calculated based on how much time it took since the signal was transmitted to when the echo was received.

For triggering the transmission HC-SR04 has a pin named Trig and for notifying the reception it uses Echo pin. Trig and Echo pins should be connected to two digital pins of Arduino. Vcc and Gnd pins of the sensor should be connected to 5V and GND pin of the Arduino respectively.

I used 4 male to female jumper wires to connect ultrasonic sensor to Arduino according to the above schematic.



Step 2: Connecting Cricket WiFi module to Arduino

Arduino has no network capability. So, we are using Cricket for connecting it to the internet to send notification to mobile application. WiFi devices are power hungry but Cricket is not! Cricket uses sleeping technique very efficiently to save power. This power efficiency and voltage compatibility made it perfect choice for battery power IoT application. Cricket goes to zero power mode automatically after sending data to the network. If you want to sent data at a regular interval, you can also configure Cricket to do it. That will wake up Cricket automatically with a given time interval by using its internal RTC (Real-Time Clock). Cricket can also wake up on a voltage provided to WAKE_UP pin from an external source and this is what we are going to use in this project.


Connect:

  • 3.3V output of the Arduino to BATT pin of the Cricket

  • digital pin 10 to the WAKE_UP pin of the Cricket

  • GND of the Arduino to the GND of Cricket


I cut 2 male to male jumper wire at the middle to make 4 and soldered three with the Cricket according to the schematic. Then I connected the Cricket with Arduino using the male header.



Step 3: Uploading program to Arduino

Lets assume we want to get notification on our phone when an object appears within the 30cm range from the sensor. To do this we need to measure the object's distance by checking the value is either 30cm or less and generate a 3.3V pulse to the Arduino digital pin. By default Arduino digital pin produce 5V which is not compatible to Cricket. So, we will use analogWrite() function to a PWM pin (pin 10 in our case) to generate a 3.3V short pulse. To generate 3.3V the analog write value is around 170.


Copy the following sketch and upload to your Arduino.

int trigPin = 8;    // Trigger
int echoPin = 9;    // Echo
int wakePin = 10;
long duration;
int cm;
 
void setup() {
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(wakePin, OUTPUT);
}
 
void loop() {
  int distance = calculate_distance();
  if(distance<=30){
     analogWrite(wakePin, 170); //produce 3.3V to wake up pin, digitalWrite() will produce 5V
     delay(10);  // make high pulse for 10ms
     analogWrite(wakePin, 0); //make it low
     delay(20000); //don't measure again untill 20seconds passed. 
    }  
  delay(500); //check every 1/2 second
}

int calculate_distance(){
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  duration = pulseIn(echoPin, HIGH);
  //We need to convert the duration to a distance. 
  //We can calculate the distance by using the following formula:
  //distance = (traveltime/2) x speed of sound
  //The speed of sound is: 343m/s = 0.0343 cm/uS = 1/29.1 cm/uS
  cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  return cm;
} 


In order to test if our program works as expected open the serial monitor and observe the reading. Place your hand or any obstacle in front of the sensor, make it closer slowly and look at the reading if the correct values are showing up.




Step 4: Configuring Pushsafer

Pushsafer is one of the easiest and reliable multi-platform push notification service. By using Pushsafer you can get 50 notification for free and then you can purchase 1000 extra for around $1. It is very easy to configure with the following steps:

  • Go to pushsafer.com and create an account

  • Login to your account and from the dashboard go to Send a Notification section

  • Write your notification message on Message box

  • Click on Create Link and write the generated link in notepad and save. We will use this link while configuring Cricket

  • Install the Pushsafer app in your smartphone and login with the same account


For testing you can press the Send Message button. If everything configured correctly you should receive a notification message in your smartphone containing the sentence you entered in the Message box.



Step 5: Configuring Cricket

Now we are going to configure Cricket to send a notification to a phone when it wakes up. This happens when WAKE_UP pin receives a short pulse (some voltage for a short time) and in our case it receives 3.3V pulse for 10ms from Arduino digital pin 10. After getting the pulse, Cricket wakes up and gets connected to the Pushsafer service by using the URL link we obtained in the previous step. Once connected, the Pushsafer service sends the notification to the phone if Pushsafer app is installed in that phone.


Let's start configuring Cricket step by step.


First we need to connect the Cricket to our WiFi network. To do this

  • Press the built-in Cricket's button for 5 seconds (it will start a new WiFi hotspot named as toe_device)

  • Connect to that hotspot from a phone or laptop

  • Set you WiFi credentials: SSID and Password and press CONNECT button.


If connected successfully, open CONFIG panel and set the following

  • CONNECTIVITY-> type: HTTP_GET

  • CONNECTIVITY-> url: [paste the URL obtained from the previous state]

  • IO1-> force update: On


Configuration may look like this:


After successful configuration we need to press the switch off button on top right corner to exit from the Cricket's configuration. After that our device is ready to go.



Step 6: Physical connection and testing

So far, we have connected the ultrasonic sensor and Cricket to Arduino, uploaded the program to Arduino, configured the Pushsafer and Cricket. We also installed the Pushsafer app to our smartphone and logged in to our account. Now, it's the right time to test it.


Power up your Arduino from either your laptop or power bank using the USB cable. Alternatively, you can use 7 to 24 volt power adaptor. I am using power bank here for testing. When I place my hand in front of the sensor within 30cm I receive a phone notification. If you receive it too then then Congratulation! You successfully made it!!





Summary

I have shown how you can make a project with Arduino and connect it to WiFi with Cricket. As Cricket has a robust connectivity options you can connect your devices to almost any internet service and local systems by using MQTT and HTTP apis. It may open up a new way of making your IoT devices. In this prototype I used Arduino, however, it is not ideal due to the size and power consumption. Other ultra-low power micro-controllers and development boards will serve better for the purpose of the final product. To mention a few: Raspberry Pi Pico, Teensy, Sparkfun Apollo3 baord, Microchip curiocity nano evalution kit, DFRobot Beetle, Texas Instrument LaunchPad boards, Maxim Integrated MAX78000FTHRI board, Maxim Integrated MAX32666FTHR Evaluation Kit, QuickLogic QuickFeather Dev Kit, Nordic nRF52840 Board etc.

Most of them can be powered directly from a battery along with Cricket. So you can make battery powered WiFi devices which can run for a very long time on single batteries (months/years). Some of the boards (e.g. Apollo3 baord, QuickFeather Dev Kit or MAX78000FTHRI board) also come with machine learning capability and consume very little power. So one of these boards and Cricket can be a perfect combination for battery operated machine learning capable IoT device.


Thank you for getting up to here! I hope you enjoyed this tutorial.


Md Khairul Alam


Recent Posts

See All
logo.png
  • LinkedIn - White Circle
  • White Facebook Icon
  • White Twitter Icon
  • White YouTube Icon
  • Instagram

Registered in United Kingdom

No. 11445097

Things On Edge Ltd

Cambridge, UK

CB25 9LT

Copyright © 2018 Things On Edge Limited

All rights reserved