UNIHIKERGeneral

Python Code Example for I2C on Unihiker

userHead O Raymos 2023-07-25 13:41:37 640 Views6 Replies

Do you have any examples on how to use the I2C function on Unihiker.  

 

I am trying to read data from a 9-DOF ICM-20948 board. 

 

There is very little detail on the pinpong library functions or examples

 

Thanks

2023-08-29 12:14:50

Warning.  I have experienced that the i2c scan takes MULTIPLE attempts before it finds my device.  The first scans return no devices.  You need to run it in a loop. After more experimentation, I found that even if i2c scan can not see the device immediately, the device IS functionsl and will respond immediately to commands.

userHeadPic GaiaTonic
2023-08-01 11:27:21

The I2C interface of the unihiker is connected to a co-processor (gd32), so the pinpong library in Python is used, which operates on the same principle as Firmata.

By examining the source code of the pinpong library on pypi, you can see that there are five functions in pinpong for I2C data read and write:

- writeto(i2c_addr, value)- writeto_mem(i2c_addr, reg, value)- readfrom(i2c_addr, read_byte)- readfrom_mem(i2c_addr, reg, read_byte)- readfrom_mem_restart_transmission(i2c_addr, reg, read_byte)

-----------------------------------------------------

Here is the link to the GitHub repository where I attempted to port the Raspberry Pi library to unihiker. It might be useful to you:https://github.com/liliang9693/DFRobot_SGP40/tree/master/Python

 

 

userHeadPic LL
2023-07-28 19:48:17

Try to test the 9-DOF ICM-20948 sensor separately. If possible with an Arduino UNO. 

userHeadPic bidrohini.bidrohini
2023-07-25 17:41:13

Hi!

 

 

# -*- coding: utf-8 -*-

 

 

 

#i2c

 

 

import time

 

 

from pinpong.board import Board,I2C

 

 

Board("UNIHIKER").begin()  #初始化,选择板型和端口号,不输入端口号则进行自动识别

 

#Board("UNIHIKER","COM36").begin()  #windows

 

 

#Board("UNIHIKER","/dev/ttyACM0").begin()   #linux

 

 

#Board("UNIHIKER","/dev/cu.usbmodem14101").begin()   #mac

 

 

 

i2c = I2C()

 

 

#i2c = I2C(bus_num=0)#bus_num default 0

 

 

l=i2c.scan()#[0x33]

 

print("i2c list:",l)

 

#i2c.writeto(0x33,[1,2,3,4,5]) 

 

#i2c.readfrom(0x33,6)#read 6 bit

 

#i2c.readfrom_mem(0x33,2,6)#i2c register 2 read 6bit

 

#i2c.writeto_mem(0x33,2,[1,2,3,4,5])#i2c register 2 write[1,2,3,4,5]

 

while(1):

 

time.sleep(1)

 

userHeadPic jenna
O Raymos wrote:

Thank you!

2023-07-27 07:15:32
O Raymos wrote:

So I tried running the code, the i2c scan is empty.  But when I i2c-tools it finds both ports.  Is there something I need to enable? 

2023-07-27 07:49:08
2 Replies