Skip to main content

Arduino and ultrasonic sensor measurement circuit with coding

Arduino project
Needed
Arduino UNO R3 =1
HC-SR04 Ultrasonic Sensor =1
Red LED =1
Green LED =1
560 ohm Resistors =2
Half Breadboard =1
Eight (8) Male/Male hookup wires =1
Connecting method

Pin configurations
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail

Copy this below code and upload in your arduino
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 4) {  // This is where the LED On/Off happens
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  digitalWrite(led2,LOW);
}
  else {
    digitalWrite(led,LOW);
    digitalWrite(led2,HIGH);
  }
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

100% tested & working sucessfully try in on your home

Comments

Popular posts from this blog

Arduino colour sensor and servo pro

Colour sensor with servo project Making All we need for this project is one color sensor (TCS3200) and two hobbyist servo motors, which makes this project quite simple but yet very fun to build it. In the first place, using the Solidworks 3D modeling software I made the design of the color sorter and here’s its working principle Initially, the colored skittles which are held in the charger drop into the platform attached on the top servo motor.Then the servo motor rotates and brings the skittle to the color sensor which detects its color.After that the bottom servo motor rotates to the particular position and then the top servo motor rotates again till the skittle drop into the guide rail. Connecting method The code is below /* Arduino Project - Color Sorting Machine * * by Dejan Nedelkovski, www.HowToMechatronics.com * */ #include <Servo.h> #define S0 2 #define S1 3 #define S2 4 #define S3 5 #define sensorOut 6 Servo topServo; Servo b...

220v ac to 5v dc without transformer

220v ac to 5v dc converted circuit Needed 4*1N4007 diode 100 ohm 1W 470k 1W 225k/400v capasitor 470uf/100v capasitor 220ohm resistor 15v 1W diode led 10uf 25v capasitor 7805 ic 1uf 10v Now the time for circuit design This is the simplest way for converting 220 ac volts to 5v dc volts (usb current) it is use for mobile phone charger and power bank charger. Capasitor in this circuit for give smooth current. Diode in this circuit for convert ac to dc. (Diode bridge)   Capasitor in this circuit. Capacitors in DC Circuits. Capacitors do not play an important role in DC circuits because it is impossible for a steady current to flow across acapacitor. If an uncharged capacitor is connected across the terminals of a battery of voltage then a transient current flows as the capacitor plates charge up. Diode in this circuit for. Diodes can be used as rectifiers, signal limiters, voltage regulators, switches, signal modulators, sign...

how to fire a matchstrick wirelessly using android phone

Copper wire:0.5mm/1mm Copper wire:0.1-0.4mm Motor Arduino esp8266 12E 5v relay module Alkaline battery:5v-9v AA battery:3v Android phone Blynk application No worry about this circuit because this is 100% working and tested. The basic consept of this circuit is below This circuit is possible because mini copper wire get more heat than the bigger wire. Codeing method First you install arduino ide on your pc Then copy and paste this code in your arduino #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> // You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon). char auth[] = "YourAuthToken"; // Your WiFi credentials. // Set password to "" for open networks. char ssid[] = "YourNetworkName"; char pass[] = "YourPassword"; void setup() {   Serial.begi...