用树莓派玩传感器

接线

VCC -> 1针脚
OUT -> 40针脚
GND -> 6针脚

红外避障传感器

C语言代码:

#include <wiringPi.h>
#include <stdio.h>
#include <sys/time.h>

#define AVOID    29

int main(void)
{
    
    

    if (wiringPiSetup() == -1)
    {
    
    
        printf("setup wiringPi failed !");
        return 1;
    }

    pinMode(AVOID, INPUT);

    while (1)
    {
    
    
        if (digitalRead(AVOID) == 0)
        {
    
    
            printf("阻挡\n");
            delay(500);
        }
    }

    return 0;
}

Python代码:

import RPi.GPIO as GPIO
import os
import time

GPIO.setmode(GPIO.BCM) #使用BCM编码方式

GPIO_OUT = 21

GPIO.setup(GPIO_OUT,GPIO.IN)


while True:
    if GPIO.input(GPIO_OUT)==0: #当有障碍物时,传感器输出低电平,所以检测低电平
        print("There has a barrier")
        time.sleep(1)
       # exit(-1)
    #else:

    #print("OK")
    #time.usleep(1000)

GPIO.cleanup()

声音传感器

声音传感器需要调一下电阻来调节传感器对声音的灵敏度

C语言:


#include <stdio.h>
#include <wiringPi.h>

#define PCF       29

int main (void)
{
    
    
        int value;
        int count = 0;
        wiringPiSetup ();

        pinMode(PCF,INPUT);
        while(1) 
        {
    
    
                if (digitalRead(PCF) != 0)
                {
    
    
                        printf("Voice In!! \n");
                        delay(500);
                }
                else{
    
    
                        printf("ok\n");
                        delay(500);
                } 
        } 
        return 0;
}        

Python:

#!/usr/bin/python
# encoding:utf-8
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
pin_voice=21
GPIO.setup(pin_voice, GPIO.IN)
try:
    while True:
        status = GPIO.input(pin_voice)
        if status == True:
            print('检测到声音')
        else:
            print('正常')
        time.sleep(0.5)
except KeyboradInterrupt:
    GPIO.cleanup()

配合电灯程序来实现吹灭LED灯操作:

#include <stdio.h>
#include <wiringPi.h>

#define PCF       29
#define LED       9

int main (void)
{
    
    
        int value;
        int count = 0;
        wiringPiSetup ();
        // Setup pcf8591 on base pin 120, and address 0x48
        pinMode(PCF,INPUT);
        while(1) // loop forever
        {
    
    
                if (digitalRead(PCF) != 0)
                {
    
    
                        printf("Voice In!! \n");
                        digitalWrite(LED,LOW);  //低电平灭
                        return 0;
                }
                else{
    
    
                        printf("ok\n");
                        digitalWrite(8,HIGH);           //高电平亮
                        delay(500);
                }
        }
        return 0;
}

火焰传感器

Python代码:

#!/usr/bin/python
# encoding:utf-8
import RPi.GPIO as GPIO
import time

pin_fire=21
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_fire, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
try:
    while True:
        status = GPIO.input(pin_fire)
        if status == True:
            print('检测到火')
        else:
            print('未检测到火')
        time.sleep(0.5)
except KeyboradInterrupt:
    GPIO.cleanup()
             

C语言:

#include <stdio.h>
#include <wiringPi.h>

#define PCF     29

int main (void)
{
    
    
        wiringPiSetup ();
        // Setup pcf8591 on base pin 120, and address 0x48
        pinMode(PCF,INPUT);
        while(1) // loop forever
        {
    
    
                if (digitalRead(PCF) != 0)
                {
    
    
                        printf("检测到火\n");
                        delay(500);
                }
                else{
    
    
                        printf("无火\n");
                        delay(500);
                }
        }
        return 0;
}

烟雾传感器

#!/usr/bin/python
# encoding:utf-8
import RPi.GPIO as GPIO
import time

pin_fire=21
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_fire, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
try:
    while True:
        status = GPIO.input(pin_fire)
        if status == True:
            print('没有检测到烟雾')
        else:
            print('检测到有烟雾')
        time.sleep(0.5)
except KeyboradInterrupt:
    GPIO.cleanup()

超声波测距传感器

#include <wiringPi.h>
#include <stdio.h>
#include <sys/time.h>

#define Trig    29
#define Echo    28

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);      //发出超声波脉冲
        digitalWrite(Trig, LOW);

        while(!(digitalRead(Echo) == 1));
        gettimeofday(&tv1, NULL);           //获取当前时间

        while(!(digitalRead(Echo) == 0));
        gettimeofday(&tv2, NULL);           //获取当前时间

        time1 = tv1.tv_sec * 1000000 + tv1.tv_usec;   //微秒级的时间
        time2  = tv2.tv_sec * 1000000 + tv2.tv_usec;

        dis = (float)(time2 - time1) / 1000000 * 34000 / 2;  //求出距离


猜你喜欢

转载自blog.csdn.net/zouchengzhi1021/article/details/114026649