We use I2C on OpenMV board using I2C library like Arduino way. PCF8591 AD/DA Converter module has three sensor devices: Thermistor, Photo-voltaic cell and Potentiometer. This module runs on I2C bus with address 0x48. In this case, we read all sensor data.
Open OpenMV IDE. You can write the following complete codes.
from machine import Pin,I2C import time print('Deomo: reading sensor from i2c protocol') PCF8591 = 0x48 # I2C bus address PCF8591_ADC_CH0 = '\x00' # thermistor PCF8591_ADC_CH1 = '\x01' # photo-voltaic cell PCF8591_ADC_CH3 = '\x03' # potentiometer # construct an I2C bus gpio_scl = Pin('P4') gpio_sda = Pin('P5') i2c = I2C(scl=gpio_scl, sda=gpio_sda, freq=100000) while 1: # read thermistor i2c.writeto(PCF8591, PCF8591_ADC_CH0) i2c.readfrom(PCF8591, 1) data = i2c.readfrom(PCF8591, 1) print('Thermistor: ' + str(ord(chr(data[0])))) # photo-voltaic cell i2c.writeto(PCF8591, PCF8591_ADC_CH1) i2c.readfrom(PCF8591, 1) data = i2c.readfrom(PCF8591, 1) print('photo-voltaic: ' + str(ord(chr(data[0])))) # potentiometer i2c.writeto(PCF8591, PCF8591_ADC_CH3) i2c.readfrom(PCF8591, 1) data = i2c.readfrom(PCF8591, 1) print('potentiometer: ' + str(ord(chr(data[0])))) time.sleep(1500)
Save this program as i2csensor.py.