板载传感器实验

test

1.气压传感器

气压传感器是用于测量气体的绝对压强的仪器,主要适用于与气体压强相关的物理实验,如气体定律等,也可以在生物和化学实验中测量干燥、无腐蚀性的气体压强。

from machine import Pin,I2C
import time
from tftlcd import LCD24
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
WHITE = (255,255,255)
d = LCD24(portrait=1)
d.fill(WHITE)
class HP203B():
    def get(i2c):
        i2c=I2C(0, scl=Pin(18), sda=Pin(17), freq=115200)
        i2c.writeto(0x77,bytearray([0x06]))#复位

        while 1:
            i2c.writeto(0x77,bytearray([0xec]))#写入指令
            i2c.writeto(0x77,bytearray([0x40]))#配置分辨率
            i2c.writeto(0x77,bytearray([0xec]))#写入指令
            i2c.writeto(0x77,bytearray([0x80]))
            i2c.writeto(0x77,bytearray([0xec]))#写入指令
            i2c.writeto(0x77,bytearray([0x30]))#只读气压
            i2c.writeto(0x77,bytearray([0xed]))#读取指令
            P=i2c.readfrom(0x77,3)
            p= ((P[0] << 16) | (P[1] << 8) | P[2])
            if(p& 0x80000):
                p |= 0xfff00000;
            print('%.2fPa'%(p/100.0))
            d.printStr('%.2fPa'%(p/100.0), 10, 10, RED, size=1)
            i2c.writeto(0x77,bytearray([0xec]))
            i2c.writeto(0x77,bytearray([0x32]))#只读温度
            i2c.writeto(0x77,bytearray([0xed]))#读取指令
            T=i2c.readfrom(0x77,3)
            t=((T[0] << 16) | (T[1] << 8) | T[2])
            if(t& 0x80000):
                t |= 0xfff00000;
            print('%.2fC'%(t/100.0))
            d.printStr('%.2fC'%(t/100.0), 10, 30, RED, size=1)
            i2c.writeto(0x77,bytearray([0xec]))
            i2c.writeto(0x77,bytearray([0x31]))#只读高度
            i2c.writeto(0x77,bytearray([0xed]))
            H=i2c.readfrom(0x77,3)
            h=((H[0] << 16) | (H[1] << 8) | H[2])
            if(h& 0x80000):
                h |= 0xfff00000;
            print('%.2fM'%(h/100.0))
            d.printStr('%.2fM'%(h/100.0), 10, 50, RED, size=1)
            time.sleep(1)
            print(' ')

2.温湿度传感器

from machine import Pin ,I2C
import time
import ahtx0
from tftlcd import LCD24
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
WHITE = (255,255,255)
d = LCD24(portrait=1)
d.fill(WHITE)
i2c = I2C(0, scl=Pin(18), sda=Pin(17), freq=115200)
sensor_AHT20=ahtx0.AHT20(i2c)
while 1:
    time.sleep_ms(1000)
    print("实时温度: %0.2f C" % sensor_AHT20.temperature)
    print("实时湿度: %0.2f %%" % sensor_AHT20.relative_humidity)   
    d.printStr("Temperature_:%0.2f C" % sensor_AHT20.temperature, 10, 20, RED, size=1)
    d.printStr("Humidity_:%0.2f %%" % sensor_AHT20.relative_humidity, 10, 50, RED, s

3.罗盘传感器

from machine import Pin ,I2C
import time
from tftlcd import LCD24
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
WHITE = (255,255,255)
d = LCD24(portrait=1)
d.fill(WHITE)
i2c = I2C(0,scl = Pin(18),sda = Pin(17),freq =115200)
QMC5883_ADDR=0xd
QMC5883_0x0b=0x01
QMC5883_0x20=0X40
QMC5883_0x21 =0x01
QMC5883_0x09 =0x0d
t1=[0x09,QMC5883_0x09,0x00,QMC5883_0x0b]
t2=[0x20,QMC5883_0x20,QMC5883_0x21]
while 1:
    i2c.writeto(QMC5883_ADDR,bytearray(t1),4)
    i2c.writeto(QMC5883_ADDR,bytearray(t2),3)
    i=i2c.readfrom(QMC5883_ADDR,8)
    hpx=i[2];
    hpx=i[3]<<8|hpx;
    hpy=i[4];
    hpy=i[5]<<8|hpy;
    hpz=i[6];
    hpz=i[7]<<8|hpz;
    hx=i[0]
    x=hpx*79.61/12000
    y=hpy*79.61/12000
    z=hpz*79.61/12000
    print('x:%.2f'%x+' y:%.2f'%y+' z:%.2f'%z)
    d.printStr('x:%.2f'%x+' y:%.2f'%y+' z:%.2f'%z, 10, 10, RED, size=1)
    time.sleep(1)

4.环境光传感器

from machine import Pin,I2C
import time
from tftlcd import LCD24
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
WHITE = (255,255,255)
d = LCD24(portrait=1)
d.fill(WHITE)
gy30_addr = 0x29 
i2c = I2C(0,scl = Pin(18),sda = Pin(17),freq =115200)
i2c.writeto(gy30_addr,bytearray([0x80,0x03]))
i2c.writeto(gy30_addr,bytearray([0x94]))
t=i2c.readfrom(gy30_addr,4)
while 1:
    gy = i2c.readfrom(gy30_addr,2)
    gy30 = float(gy[0] << 8 | gy[1])/240
    time.sleep(1)
    print('%.2f'%gy30)
    d.printStr('%.2f'%gy30, 10, 10, RED, size=1)

5.超声波传感器

from machine import Pin
import time
from tftlcd import LCD24
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
WHITE = (255,255,255)
d = LCD24(portrait=1)
d.fill(WHITE)
trig = Pin(36,Pin.OUT)
echo = Pin(35, Pin.IN)
trig.off()
echo.off()
def fasong():
    trig.on()
  time.sleep_us(10)
  trig.off()
  while(echo.value() == 0):
    pass
    t1 = time.ticks_us()
  while(echo.value() == 1):
    pass
    t2 = time.ticks_us()
  t3 = time.ticks_diff(t2,t1)/10000
  return t3*340/2
try:
    while 1:
        print("障碍物距离为:%0.2f cm" %fasong())
        d.printStr("Distance:%3.2f cm" %fasong(), 10, 10, RED, size=2)
        time.sleep(1)
except KeyboardInterrupt:
    pass

6.红外热释电传感器

import time
from neopixel import NeoPixel
from machine import SoftI2C,Pin   #从machine模块导入I2C、Pin子模块
Human=Pin(1,Pin.IN,Pin.PULL_UP)   #构建人体红外对象
pin = Pin(0, Pin.OUT)             # 设置引脚GPIO0来驱动 NeoPixels
np = NeoPixel(pin, 4)   

while True: 
    if Human.value()==1:
        np[1] = (10, 0, 0)
        np.write()
    else :
        np[1] = (0, 0, 0)
        np.write()

7.重力陀螺仪传感器

from machine import I2C,Pin
import time
import qmi8658
qmi8658=qmi8658.IMU_QMI8658C()
from tftlcd import LCD24
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
WHITE = (255,255,255)
d = LCD24(portrait=1)
d.fill(WHITE)
while 1:
    xyz=qmi8658.Read_XYZ()
    time.sleep(1)
    print("ACC_X=%.2f"%xyz[0])
    print("ACC_Y=%.2f"%xyz[1])
    print("ACC_Z=%.2f"%xyz[2])
    print("GYR_X=%3.2f"%(xyz[3]+15))
    print("GYR_Y=%3.2f"%xyz[4])
    print("GYR_Z=%3.2f"%xyz[5])
    print(' ')
    d.printStr("ACCX=%.2f"%xyz[0], 10, 10, RED, size=1)
    d.printStr("ACCY=%.2f"%xyz[1], 10, 30, RED, size=1)
    d.printStr("ACCZ=%.2f"%xyz[2], 10, 50, RED, size=1)
    d.printStr("GYRX=%3.2f"%(xyz[3]+15), 10, 70, RED, size=1)
    d.printStr("GYRY=%3.2f"%xyz[4], 10, 90, RED, size=1)
    d.printStr("GYRZ=%3.2f"%xyz[5], 10, 110, RED, size=1)