【树莓派+arduino实验记录21】超声波距离传感器

2020-09-15  本文已影响0人  Geekero

Arduino

//------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second
//----------------------------------------------------
//initialize the library code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
#define TRIGGER_PIN  2 // Arduino pin tied to the trigger pin on the ultrasonic sensor.
#define ECHO_PIN  3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE  400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //NewPing setup of pins and maximum distance.

void setup(){
  Serial.begin(115200); //Open serial monitor at 115200 baud to see ping results.
  lcd.init();
  lcd.backlight();
}

void loop(){
  delay(100); //wait for 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay bwtween pings.
  unsigned int uS = sonar.ping(); //send pings, get ping time in microseconds(uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm"); 
  lcd.setCursor(0, 0);
  lcd.print("Distance:");
  lcd.setCursor(0, 1);
  lcd.print("                ");
  lcd.setCursor(9, 1);
  lcd.print(uS / US_ROUNDTRIP_CM);
  lcd.setCursor(12, 1);
  lcd.print("cm");
}

树莓派

C

/*************************************
 * Ultra Sonic Ranging module Pin VCC should
 * be connected to 5V power.
 * *********************************/
#include <wiringPi.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#define Trig  0
#define Echo  1

void ultraInit(void)
{
    pinMode(Echo, INPUT);
    pinMode(Trig, OUTPUT);
}

float disMeasure(void)
{
    struct timeval tv1;
    struct timeval tv2;
    long time1, time2;
    float dis;
    digitalWrite(Trig, LOW);
    delayMicroseconds(2);
    digitalWrite(Trig, HIGH);
    delayMicroseconds(10); //send out ultrasonic pulses

    while(!(digitalRead(Echo) == 1));
    gettimeofday(&tv1, NULL); //get current time
    while(!(digitalRead(Echo) == 0));
    gettimeofday(&tv2, NULL);
    time1 = tv1.tv_sec * 1000000 + tv1.tv_usec; //microsecond time
    time2 = tv2.tv_sec * 1000000 + tv2.tv_usec;
    dis = (float)(time2 - time1)/1000000 * 34000 / 2; //calculate the distance
    return dis;
}

int main(void)
{
    float dis;
    if (wiringPiSetup() == -1){
        printf("setup wiringPi failed !");
        return 1;
    }
    ultraInit();

    while(1){
    dis = disMeasure();
    printf("%0.2f cm\n\n", dis);
    delay(300);
    }
    return 0;
}

Python

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
TRIG = 11
ECHO = 12

def setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(TRIG, GPIO.OUT)
    GPIO.setup(ECHO, GPIO.IN)

def distance():
    GPIO.output(TRIG, 0)
    time.sleep(0.000002)
    GPIO.output(TRIG, 1)
    time.sleep(0.00001)
    GPIO.output(TRIG, 0)

    while GPIO.input(ECHO) == 0:
        a = 0
        time1 = time.time()
    while GPIO.input(ECHO) == 1:
        a = 1
        time2 = time.time()
        
    during = time2 - time1
    return during * 340 / 2 * 100

def loop():
    while True:
        dis = distance()
        print('{:.2f}cm'.format(dis))
        print('')
        time.sleep(0.3)

def destroy():
    GPIO.cleanup()


if __name__ == '__main__':
    setup()
    try:
        loop()
    except KeyboardInterrupt:
        destroy()
上一篇下一篇

猜你喜欢

热点阅读