Forum >Replies by Paul.Holloway
userhead Paul.Holloway
Replies (2)
  • You Reply:

    I finally got round to getting it to work on I2C. Still no luck with UART. But I2C works nicely. The code that works on RP pico using micropython is below in case anyone needs it:

     

    from machine import Pin, I2Cimport timeI2C_interface=(0)sdapin= Pin(0)sclpin= Pin(1)

    i2c = I2C(I2C_interface, scl=sclpin, sda=sdapin,freq=100000) OXYGEN_I2C_ADDRESS = 0x74sendbuf = bytearray(9)  recvbuf = bytearray(9)O2Reading = 0.0

    def clear_buffer(buf,length): for i in range(0,length):   buf[i]=0

    #check O2 Sensor connectiontry:   i2c.readfrom(OXYGEN_I2C_ADDRESS, 1)   print("I2C O2 connect success!")except OSError:   print("I2C O2 device error!")

     

    def fuc_check_sum(i,ln): tempq=0 for j in range(1,ln-1):   tempq+=i[j] tempq=(((~tempq)&0xff)**) return tempq

    # 0x04 is used to set O2 sensor to passive mode. Then check if recvbuf[2]=1 to see if mode change is completeO2mode_Passive = bytearray([0xFF, 0x01, 0x78, 0x04, 0x00, 0x00, 0x00, 0x00, fuc_check_sum(sendbuf,8)])i2c.writeto(0x74, O2mode_Passive)time.sleep(0.1)i2c.readfrom_into(0x74, recvbuf)if(recvbuf[2]==1):   print ("O2 mode changed to passive")else:   print ("Failed to change O2 to passive")

    def O2Read():   global recvbuf   clear_buffer(recvbuf,9)   CheckO2 = bytearray([0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, fuc_check_sum(sendbuf,8)])   i2c.writeto(0x74, CheckO2)   time.sleep(0.1)   recvbuf = bytearray(9)   i2c.readfrom_into(0x74, recvbuf)   if(fuc_check_sum(recvbuf,8) == recvbuf[8]):     O2Reading = ((recvbuf[2]<<8)+recvbuf[3])*1.0     decimal_digits = recvbuf[5]     if decimal_digits == 1:       O2Reading = O2Reading * 0.1       return O2Reading      elif decimal_digits == 2:       O2Reading = O2Reading * 0.01       return O2Reading    else: # Read failure. Readings of 100 concidered failure     O2Reading = 100     return O2Reading

    while True:   O2 = O2Read()   print (O2)   time.sleep(1) 

     

  • You Reply:

    Thanks @jenna! I'll give this a go to see if I get a response. I've never tried I2C before, so the resource is very useful. I'll let you know how I get on.