how to make taking distance measurement device using Arduino and ultrasonic sensor
Homemade Talking measuring Device using Arduino , Ultrasonic and Speaker
Namskar dooston ajke is article me hum sikhenge kaise hum ek distance measurement device banayenge jiske madad se hum kiska bhi distance measure kar sakte hai wo bhi bina touch kiye aur ye device apko bolke batayega distance kitna hai. agar apko project banane me koi bhi problems hota hai iske video jarur dekhe jo apko niche miljayega agar ap video sahise full nahi dekhoge apko kuchh bhi samhme nahi ayega..
Components Required.
1. Arduino Uno or Nano.
2. Mini Amplifier.
3. Speaker.
4. Ultrasonic sensor.
5. Toggle Switch.
6. 3.7V Li-ion Battery.
7. Li-ion battery charger.
8 PVC sheet.
CIRCUIT DIAGRAM.
Making Video
MAIN ARDUINO CODE.
#include <Arduino.h>
#include "TalkieUtils.h"
#include "Vocab_US_Large.h"
#include "Vocab_Special.h"
#include "HCSR04.h"
Talkie Voice;
const uint8_t ECHO_IN_PIN = 9;
const uint8_t TRIGGER_OUT_PIN = 10;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_USB) || defined(SERIAL_PORT_USBVIRTUAL) || defined(ARDUINO_attiny3217)
delay(4000);
#endif
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_TALKIE));
initUSDistancePins(TRIGGER_OUT_PIN, ECHO_IN_PIN);
#if defined(TEENSYDUINO)
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
#endif
Serial.print("Voice queue size is: ");
Serial.println(Voice.sayQ(spPAUSE1));
}
void loop() {
int tCentimeter = getUSDistanceAsCentiMeterWithCentimeterTimeout(300);
// print distance
if (tCentimeter >= 300) {
Serial.println("timeout");
sayQTimeout(&Voice);
Voice.sayQ(sp2_OUT);
} else {
Serial.print("cm=");
Serial.println(tCentimeter);
float tDistanceMeter = tCentimeter / 100.0;
sayQFloat(&Voice, tDistanceMeter, 2, true, true);
Voice.sayQ(sp2_METER);
}
Voice.sayQ(spPAUSE1);
while (Voice.isTalking()) {
;
}
}
ATTACHED FILE
HCSR04.cpp
#include <Arduino.h>
#include "HCSR04.h"
uint8_t sTriggerOutPin; // also used as aTriggerOutEchoInPin for 1 pin mode
uint8_t sEchoInPin;
uint8_t sHCSR04Mode = HCSR04_MODE_UNITITIALIZED;
void initUSDistancePins(uint8_t aTriggerOutPin, uint8_t aEchoInPin) {
sTriggerOutPin = aTriggerOutPin;
if (aEchoInPin == 0) {
sHCSR04Mode = HCSR04_MODE_USE_1_PIN;
} else {
sEchoInPin = aEchoInPin;
pinMode(aTriggerOutPin, OUTPUT);
pinMode(sEchoInPin, INPUT);
sHCSR04Mode = HCSR04_MODE_USE_2_PINS;
}
}
void initUSDistancePin(uint8_t aTriggerOutEchoInPin) {
sTriggerOutPin = aTriggerOutEchoInPin;
sHCSR04Mode = HCSR04_MODE_USE_1_PIN;
}
unsigned int getUSDistance(unsigned int aTimeoutMicros) {
if (sHCSR04Mode == HCSR04_MODE_UNITITIALIZED) {
return 0;
}
digitalWrite(sTriggerOutPin, HIGH);
if (sHCSR04Mode == HCSR04_MODE_USE_1_PIN) {
pinMode(sTriggerOutPin, OUTPUT);
}
#ifdef DEBUG
delayMicroseconds(100);
#else
delayMicroseconds(10);
#endif
digitalWrite(sTriggerOutPin, LOW);
uint8_t tEchoInPin;
if (sHCSR04Mode == HCSR04_MODE_USE_1_PIN) {
delayMicroseconds(20);
pinMode(sTriggerOutPin, INPUT);
tEchoInPin = sTriggerOutPin;
} else {
tEchoInPin = sEchoInPin;
}
#if ! defined(__AVR__) || defined(TEENSYDUINO) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
noInterrupts();
unsigned long tUSPulseMicros = pulseIn(tEchoInPin, HIGH, aTimeoutMicros);
interrupts();
#else
unsigned long tUSPulseMicros = pulseInLong(tEchoInPin, HIGH, aTimeoutMicros);
#endif
return tUSPulseMicros;
}
unsigned int getCentimeterFromUSMicroSeconds(unsigned int aDistanceMicros) {
// The reciprocal of formula in getUSDistanceAsCentiMeterWithCentimeterTimeout()
return (aDistanceMicros * 100L) / 5825;
}
unsigned int getUSDistanceAsCentiMeter(unsigned int aTimeoutMicros) {
return (getCentimeterFromUSMicroSeconds(getUSDistance(aTimeoutMicros)));
}
// 58,23 us per centimeter (forth and back)
unsigned int getUSDistanceAsCentiMeterWithCentimeterTimeout(unsigned int aTimeoutCentimeter) {
// The reciprocal of formula in getCentimeterFromUSMicroSeconds()
unsigned int tTimeoutMicros = ((aTimeoutCentimeter * 233L) + 2) / 4;
return getUSDistanceAsCentiMeter(tTimeoutMicros);
}
void testUSSensor(uint16_t aSecondsToTest) {
for (long i = 0; i < aSecondsToTest * 50; ++i) {
digitalWrite(sTriggerOutPin, HIGH);
delayMicroseconds(582);
digitalWrite(sTriggerOutPin, LOW);
delay(20);
}
}
#if (defined(USE_PIN_CHANGE_INTERRUPT_D0_TO_D7) | defined(USE_PIN_CHANGE_INTERRUPT_D8_TO_D13) | defined(USE_PIN_CHANGE_INTERRUPT_A0_TO_A5))
unsigned int sUSDistanceCentimeter;
volatile unsigned long sUSPulseMicros;
volatile bool sUSValueIsValid = false;
volatile unsigned long sMicrosAtStartOfPulse;
unsigned int sTimeoutMicros;
void handlePCInterrupt(uint8_t aPortState) {
if (aPortState > 0) {
// start of pulse
sMicrosAtStartOfPulse = micros();
} else {
// end of pulse
sUSPulseMicros = micros() - sMicrosAtStartOfPulse;
sUSValueIsValid = true;
}
#ifdef DEBUG
#endif
}
#endif // USE_PIN_CHANGE_INTERRUPT_D0_TO_D7 ...
#if defined(USE_PIN_CHANGE_INTERRUPT_D0_TO_D7)
ISR (PCINT2_vect) {
// read pin
uint8_t tPortState = (*portInputRegister(digitalPinToPort(sEchoInPin))) & bit((digitalPinToPCMSKbit(sEchoInPin)));
handlePCInterrupt(tPortState);
}
#endif
#if defined(USE_PIN_CHANGE_INTERRUPT_D8_TO_D13)
ISR (PCINT0_vect) {
// check pin
uint8_t tPortState = (*portInputRegister(digitalPinToPort(sEchoInPin))) & bit((digitalPinToPCMSKbit(sEchoInPin)));
handlePCInterrupt(tPortState);
}
#endif
#if defined(USE_PIN_CHANGE_INTERRUPT_A0_TO_A5)
ISR (PCINT1_vect) {
// check pin
uint8_t tPortState = (*portInputRegister(digitalPinToPort(sEchoInPin))) & bit((digitalPinToPCMSKbit(sEchoInPin)));
handlePCInterrupt(tPortState);
}
#endif
#if (defined(USE_PIN_CHANGE_INTERRUPT_D0_TO_D7) | defined(USE_PIN_CHANGE_INTERRUPT_D8_TO_D13) | defined(USE_PIN_CHANGE_INTERRUPT_A0_TO_A5))
void startUSDistanceAsCentiMeterWithCentimeterTimeoutNonBlocking(unsigned int aTimeoutCentimeter) {
digitalWrite(sTriggerOutPin, HIGH);
sUSValueIsValid = false;
sTimeoutMicros = ((aTimeoutCentimeter * 233) + 2) / 4;
*digitalPinToPCMSK(sEchoInPin) |= bit(digitalPinToPCMSKbit(sEchoInPin));
PCICR |= bit(digitalPinToPCICRbit(sEchoInPin));
PCIFR |= bit(digitalPinToPCICRbit(sEchoInPin));
sUSPulseMicros = 0;
sMicrosAtStartOfPulse = 0;
#ifdef DEBUG
delay(2);
#else
delayMicroseconds(10);
#endif
digitalWrite(sTriggerOutPin, LOW);
}
bool isUSDistanceMeasureFinished() {
if (sUSValueIsValid) {
sUSDistanceCentimeter = getCentimeterFromUSMicroSeconds(sUSPulseMicros);
return true;
}
if (sMicrosAtStartOfPulse != 0) {
if ((micros() - sMicrosAtStartOfPulse) >= sTimeoutMicros) {
*digitalPinToPCMSK(sEchoInPin) &= ~(bit(digitalPinToPCMSKbit(sEchoInPin)));
return true;
}
}
return false;
}
#endif
HCSR04.h
#ifndef HCSR04_H_
#define HCSR04_H_
#include <stdint.h>
#define US_DISTANCE_DEFAULT_TIMEOUT_MICROS 20000
#define US_DISTANCE_DEFAULT_TIMEOUT_CENTIMETER 343
#define US_DISTANCE_TIMEOUT_MICROS_FOR_1_METER 5825
#define US_DISTANCE_TIMEOUT_MICROS_FOR_2_METER 11650
#define US_DISTANCE_TIMEOUT_MICROS_FOR_3_METER 17475
void initUSDistancePins(uint8_t aTriggerOutPin, uint8_t aEchoInPin = 0);
void initUSDistancePin(uint8_t aTriggerOutEchoInPin);
unsigned int getUSDistance(unsigned int aTimeoutMicros = US_DISTANCE_DEFAULT_TIMEOUT_MICROS);
unsigned int getCentimeterFromUSMicroSeconds(unsigned int aDistanceMicros);
unsigned int getUSDistanceAsCentiMeter(unsigned int aTimeoutMicros = US_DISTANCE_DEFAULT_TIMEOUT_MICROS);
unsigned int getUSDistanceAsCentiMeterWithCentimeterTimeout(unsigned int aTimeoutCentimeter);
void testUSSensor(uint16_t aSecondsToTest);
#if (defined(USE_PIN_CHANGE_INTERRUPT_D0_TO_D7) | defined(USE_PIN_CHANGE_INTERRUPT_D8_TO_D13) | defined(USE_PIN_CHANGE_INTERRUPT_A0_TO_A5))
void startUSDistanceAsCentiMeterWithCentimeterTimeoutNonBlocking(unsigned int aTimeoutCentimeter);
bool isUSDistanceMeasureFinished();
extern unsigned int sUSDistanceCentimeter;
extern volatile unsigned long sUSPulseMicros;
#endif
#define HCSR04_MODE_UNITITIALIZED 0
#define HCSR04_MODE_USE_1_PIN 1
#define HCSR04_MODE_USE_2_PINS 2
extern uint8_t sHCSR04Mode;
#endif
#pragma once
Comments
Post a Comment