PiicoDev Unified on a Raspberry Pi Zero W

It’s good that the PiicoDev Libraries work with Python 3 on Raspberry Pis as well as with Micropython (New video by Michael; The Factory | PiicoDev® Porting To Raspberry Pi & Pressure Sensor Prototype).

After installing PiicoDev Unified on a Raspberry Pi Zero W with Raspberry Pi OS Bookworm I ran the Button Check code from ‘https://core-electronics.com.au/guides/piicodev-button-getting-started-guide/’ and it worked but these warnings preceded the code output:
'I2C is not enabled. To enable, run “sudo curl -L https://piico.dev/i2csetup | bash”. Suppress this warning by setting suppress_warnings=True
Slow baudrate detected. If glitching occurs, run “sudo curl -L https://piico.dev/i2csetup | bash”. Suppress this warning by setting suppress_warnings=True

I had already enabled I2C using raspi-config but I followed the instruction anyway, executing sudo curl -L https://piico.dev/i2csetup | bash at a linux Terminal on the Zero. After rebooting I ran the button code again and once again it worked but with a different message preceding the code output this time:
** Incorrect device found at address 66**

What’s happening here is that i2csetup.sh is pointing to the old location of /boot/config.txt but Raspberry Pi OS now has this file at /boot/firmware/config.txt.
Running i2csetup.sh changes the file in the old location - there is still a file there with that name but it’s a placeholder pointing to the new location. The library code must be looking at that config file because it detects I2C as disabled when it isn’t and after running i2csetup.sh it detects the baud rate as 400,000 when it is still at the OS default of 100,000. That is presumably the reason for the incorrect device error. When I edited /boot/firmware/config.txt to change baud rate for real that error doesn’t occur any more. So both config.txt files need to be edited to prevent the warning messages but /boot/firmware/config.txt defines the actual settings, and i2csetup.sh doesn’t modify that. I get the same results with a Raspberry Pi 3 A+.

I installed the library latest version into a virtual machine via Thonny this week and I also checked i2csetup.sh on the Github repository. Hopefully I didn’t miss something obvious.

Interesting that I’ve only seen the incorrect device error with the PiicoDev button. I tried the PiicoDev accelerometer, slide and rotary potentiometers and the OLED display before editing /boot/firmware/config.txt, without seeing that Incorrect device error.

Peter

3 Likes

Hi @Peter266808, Welcome to the Forums!

Great find! Thanks for pointing out this issue so thoroughly, It made our testing and troubleshooting quite simple.

You are spot on with the config file location change causing the issue. We have put together and are currently testing a new version of the script that will avoid this issue by doing a version check first to make sure theconfig file is in the right place so that this issue can be avoided for others in the future.

The Incorrect device found at address 66 error is a bit of a weird one. Looking into the PiicoDev_Switch.py code this seems to be finding the button at the correct address (decimal value is 66, hex value is 0x42 which matches the i2c address of the module). This error in confirming that this device is correct seems to be related to the i2c setup issue and stops for us when we correctly direct the https://piico.dev/i2csetup file to /boot/firmware/config.txt.

Thanks for bringing this to our attention, we will give you an update when we have this fix finalized!

3 Likes

Hi @Peter266808,

We have updated the i2csetup script to accommodate more recent versions of the Pi OS. Would you be able to help us field-test this change? replacing the contents of https://piico.dev/i2csetup (i2csetup.sh) with the code below should do a better job of handling the differences in the location of the config file between versions.

echo ""

echo "The script will run in 10 seconds.   Press any key to continue immediately.   Hit CTRL+C to abort"
echo "This script will modify the /boot/firmware/config.txt (or /boot/config.txt for pre bookworm versions) file to enable I2C and set the correct baudrate (400kbps). "
echo "If you want to view the contents of this script, save it to a file. Run: curl -L https://piico.dev/i2csetup > i2c-setup.sh"

# "-t 10" read for 10 seconds, "-n 1" continue if 1 character is pressed, "-s" don't output read value, "-p" provide a text output
read -t 10 -n 1 -s

# Use awk command, find the line starting with 'VERSION_ID', break line into two values, grab the second value and format for storage as an int
version_id=$(awk -F'=' '/^VERSION_ID/{gsub(/"/,"",$2);print $2}' /etc/os-release)

# Checking if the version_id call was successful
if [ $? -eq 0 ] && [ -z "$version_id" ]; then
	echo "Failed to identify the major software version"
    exit 10
else 
    echo "Version_Id: $version_id"
fi

if [ "$version_id" -lt 12 ]; then
	echo "Legacy version detected (pre version 12)"
	
	if [ ! -e /boot/config.txt ]; then
		echo "/boot/config.txt does not exist"
	else
		# This will remove dtparam=i2c_arm=
		sudo sed -i.bak '/^dtparam=i2c_arm=/d' /boot/config.txt
	
		# This will remove dtparam=i2c_arm_baudrate=
		sudo sed -i '/^dtparam=i2c_arm_baudrate=/d' /boot/config.txt
	
		# This will remove comment
		sudo sed -i '/^# Added by PiicoDev:/d' /boot/config.txt
	
		# This will add dtparam=i2c_arm=on and dtparam=i2c_arm_baudrate=400000
		sudo sed -i '$a# Added by PiicoDev: require I2C and 400k baudrate\ndtparam=i2c_arm=on\ndtparam=i2c_arm_baudrate=400000' /boot/config.txt
	
		echo "Setup complete. Please reboot."
	fi
	
else
	echo "version 12 or higher detected"
	
	if [ ! -e /boot/firmware/config.txt ]; then
		echo "/boot/firmware/config.txt does not exist"
	else
		# This will remove dtparam=i2c_arm=
		sudo sed -i.bak '/^dtparam=i2c_arm=/d' /boot/firmware/config.txt
	
		# This will remove dtparam=i2c_arm_baudrate=
		sudo sed -i '/^dtparam=i2c_arm_baudrate=/d' /boot/firmware/config.txt
	
		# This will remove comment
		sudo sed -i '/^# Added by PiicoDev:/d' /boot/firmware/config.txt
	
		# This will add dtparam=i2c_arm=on and dtparam=i2c_arm_baudrate=400000
		sudo sed -i '$a# Added by PiicoDev: require I2C and 400k baudrate\ndtparam=i2c_arm=on\ndtparam=i2c_arm_baudrate=400000' /boot/firmware/config.txt
	
		echo "Setup complete. Please reboot."
	fi	
fi

I would be really interested to see how this goes for you. Hopefully, this stops this from tripping people up in the future! :grinning:

2 Likes

Hi Sam,

Nice work with the update. It works for me now.

I did a clean install with a Raspberry Pi Zero 2 W and the exact same HW and SW configuration as before. I copied the text above into a new file i2csetup.sh in the Zero 2 W, made it executable with chmod +x i2csetup.sh and executed it with sh i2csetup.sh.

After rebooting there’s no incorrect device error as before. The new I2C lines in config.txt are down the bottom of the file, separate from the other interface lines, makes them harder to notice, but that’s inconsequential to function.

Good solution with the OS version detection in the bash script.

Peter

2 Likes

Nice @Peter266808 - thanks for being our willing Alpha tester :smiley: I’m glad it’s as simple as the new location for the config.txt file.

We’ll push this new code to the repo so other users will have the intended experience.

Happy Making! and thanks again!

@Peter266808 & @Samuel the updated script is now live at scripts/i2c-setup.sh
The setup command will now execute this version and work with Bookworm.

I refactored the code slightly to reduce code duplication and improve user experience.
Thanks guys :slight_smile:

1 Like

Such a red-herring eh? The PiicoDev “smart” modules (modules that use an in-house programmed ATtiny to handle I2C comms) are the entire reason we had to go down this route. We found that bus comms were unreliable at lower baudrates, and would result in incorrect values being returned. In this case the device ID. The funny thing is that the behaviour is deterministic so i suspect a clock-speed / division related issue.
This doesn’t affect purpose-built I2C compliant devices since they are designed at the hardware layer to be clocked at any I2C compatible speed.
Also - this problem only seems to pertain to Raspberry Pi - lower baudrates on eg. the Raspberry Pi Pico do not produce the same effect of corrupting the data.

1 Like