美文网首页
【树莓派+arduino实验记录14 】光敏传感器

【树莓派+arduino实验记录14 】光敏传感器

作者: Geekero | 来源:发表于2020-09-02 20:34 被阅读0次

Arduino

/******************************************
 * name: Photoswitch
 * function:hold the photoswitch with your fingers and check the value at A0 on Serial Monitor.
 * You can see when the resistance is up to 400ohm.
 * the normally open contact of the relay is closed and the led connected to pin 13 on the Arduino Uno board lights up
 * or else, it keeps out.
 *******************************************/
 const int photocellPin = A0; //The photoresistor module attach to A0
 const int ledPin = 13; //pin 13 built-in led
 const int relayPin = 8; //relay module attach to digital 8
 int outputValue = 0;
 /********************************************/
 void setup()
 {
  pinMode(relayPin, OUTPUT); 
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
 }
 /*********************************************/
 void loop()
 {
  outputValue = analogRead(photocellPin); //read the value of photoresistor
  Serial.println(outputValue);
  if (outputValue >= 400) 
  {
    digitalWrite(ledPin, HIGH); //turn on
    digitalWrite(relayPin, LOW); //relay connected
  }
  else
  {
    digitalWrite(ledPin, LOW);
    digitalWrite(relayPin, HIGH);
  }
  delay(1000); 
 }

树莓派

C

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

#define  PCF  120
#define  D0pin 0

int main()
{
    int analogVal;

    if (wiringPiSetup() == -1){
        printf("setup wiringPi failed !");
        return 1;
    }
    //Setup pcf8591 on base pin 120, and address 0x48
    pcf8591Setup(PCF, 0x48);

    while(1)
    {
        analogVal = analogRead(PCF + 0);
        printf("Value: %d\n", analogVal);

        delay(200);
    }
    return 0;
}

Python

#!/usr/bin/env python
import PCF8591 as ADC
import RPi.GPIO as GPIO
import time
D0 = 17

GPIO.setmode(GPIO.BCM)

def setup():
    ADC.setup(0x48)
    GPIO.setup(D0, GPIO.IN)

def loop():
    status = 1
    while True:
        print("Value: {}".format(ADC.read(0)))

        time.sleep(0.2)


if __name__ == '__main__':
    try:
        setup()
        loop()
    except KeyboardInterrupt:
        pass

相关文章

网友评论

      本文标题:【树莓派+arduino实验记录14 】光敏传感器

      本文链接:https://www.haomeiwen.com/subject/eguwsktx.html