Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Md. Khairul Alam
Published © MIT

Arduino & Cricket with Ultrasonic Sensor

Bring internet connectivity to Arduino using Cricket.

BeginnerProtip2 hours1,439
Arduino & Cricket with Ultrasonic Sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
IOT Cricket Wi-Fi module
Things On Edge IOT Cricket Wi-Fi module
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematic Diagram

Code

Arduino Code

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;
} 

Credits

Md. Khairul Alam
70 projects • 593 followers
Developer, Maker & Hardware Hacker. Currently working as a faculty at the University of Asia Pacific, Dhaka, Bangladesh.
Contact

Comments

Please log in or sign up to comment.