I had a bit of a struggle with this so thought it was worth documenting. The problem is this – the i2c bus on the Raspberry Pi is used by the official camera to initialise it. So if you want to use an i2c device at the same time as the camera, the device will stop working after a few minutes. Here’s more on this problem.
I really wanted to use this heatsensor with mynaturewatch to see if we could exclude some of the problem with false positives (trees waving in the breeze and similar). I’ve not got it working well enough yet to look at this problem in detail. But, I did get it working with the 12c bus with the camera – here’s how.
It’s pretty straightforward. You need to
- Create a new i2c bus on some different GPIOs
- Tell the library you are using for the non-camera i2c peripheral to use these instead of the default one
1. Create a new i2c bus on some different GPIOs
This is super-easy:
sudo nano /boot/config.txt
Add the following line of code, preferable in the section where spi and i2c is enabled.
This line will create an aditional i2c bus (bus 3) on GPIO 23 as SDA and GPIO 24 as SCL (GPIO 23 and 24 is defaults)
2. Tell the library you are using for the non-camera i2c peripheral to use these instead of the default one
pip3 install Adafruit_CircuitPython_AMG88xx
While the pi is switched off, plug in the i2c device using pins 23 for SDA and GPIO 24 for SDL, and then boot it up and check it’s working:
i2cdetect -y 3
Make two changes:
and change the SDA and SCL pins to the new pins
#SDA = Pin(2) #SCL = Pin(3) SDA = Pin(23) SCL = Pin(24)
Change line 21 or thereabouts to use the i2c bus 3 rather than the default, 1:
self._i2c_bus = smbus.SMBus(3)
Start up your camera code and your i2c peripheral. They should run happily together.