How to get data from I2C device BH1750 over USB-I2C module in Python3?

后端 未结 1 610
南旧
南旧 2021-01-25 18:14

I am Linux user and I have those devices:
USB-I2C convertor:
https://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm
GY-30 I2C module:
https://5.imimg.com/data5

相关标签:
1条回答
  • 2021-01-25 19:00

    There is final solution:

    import serial
    import time
    
    ser = serial.Serial(port="/dev/ttyUSB0",
                        baudrate=19200,
                        parity=serial.PARITY_NONE,
                        stopbits=serial.STOPBITS_TWO,
                        bytesize=serial.EIGHTBITS,
                        timeout=0.500,
                        )
    
    ser.flushInput()
    ser.flushOutput()
    
    ADDR_BASE = 0x23 # nebo 0x5c pokud je ADDR pin HIGH
    ADDR_READ = ADDR_BASE << 1 | 1
    ADDR_WRITE = ADDR_BASE << 1
    
    CMD_WRITE_1B = 0x53
    CMD_READ_MULT = 0x54
    
    LUX_RESOLUTION_1X = 0x10
    LUX_RESOLUTION_4X = 0x13
    
    # nastavení senzoru
    ser.write(bytearray([CMD_WRITE_1B, ADDR_WRITE, LUX_RESOLUTION_1X]))
    
    while True:
        time.sleep(1)
    
        # požádám převodník o 2 B
        ser.write(bytearray([CMD_READ_MULT, ADDR_READ, 0x02]))
    
        # přečtu je
        raw = ser.read(2)
    
        # interpretace bytů podle datasheetu
        lx = (raw[1] << 8 | raw[0]) / 1.2
    
        print("--> %.d lx" %(lx))
    
    0 讨论(0)
提交回复
热议问题