1

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/TY/AK/MY-1833510/gy-30-bh1750-intensity-digital-light-sensor-module.pdf
And GY-30 is with BH1750 Ambient light sensor:
https://www.mylms.cz/wp-content/uploads/2017/07/bh1750-datasheet.pdf

I need to read Luxs from BH1750 over virtual serial port in Python3, but I am not sure how to do it. I ended with something like this:

import serial
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()

ser.write(bytearray([0x55, 0x23, 0x11, 0x01]))
test = ser.read()

But I am not sure, what are the right bytes. I have the GY-30 connected directly to USB-I2C.

Thank for your eventual help ...

Jiri

Jiří
  • 43
  • 7
  • What machine's architecture you are running on? (For x86 case you may read this to get some interesting info: https://stackoverflow.com/q/60105101/2511795) Do you have a driver for your USB-to-I2C convertor? (Btw, in above mentioned case I connected BMP085 pressure sensor to x86 machine using Diolan USB-to-I2C converter). – 0andriy Apr 04 '20 at 15:35
  • @0andriy thanks for your answer, my machine is x86_64, but my driver is OK, I can communicate with the USB-I2C convertor (I am able to turn its LED off or get from bus info about its firmware version - for example). The problem is, that I am not able to communicate with the I2C connected module, because I am not sure, what exactly and in what order I need to send bytes ... – Jiří Apr 04 '20 at 17:02
  • @0andriy yes, I read it, but I am not sure about some parts, because of that I made this question, I need right bytes and its order and I am not able to make it by myself ... – Jiří Apr 04 '20 at 17:51
  • @0andriy yes, that was my plan, to USB-I2C send 0x55, then 0x23, which is adress of GY-30, then 0x01 which is info to enable BH1750 .... etc, etc, etc. But it is not working, so I need help ... – Jiří Apr 04 '20 at 17:59
  • Second byte is address + command as far as I can see. So, if 7-bit address is 0x23, to read from it you have to use _(0x23 << 1) | 1_. – 0andriy Apr 04 '20 at 18:02
  • @andriy I am not sure if I understand .... can you write it like command or bytearray? – Jiří Apr 04 '20 at 18:21
  • In i2c protocol the first byte is a 7-bit address of slave device (bits 7..1) + one bit (bit 0) of command: read or write. Looks like you probably need to read a bit about i2c communications. – 0andriy Apr 04 '20 at 18:51

1 Answers1

1

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))
Jiří
  • 43
  • 7