User:Istitov/Pimoroni Enviro pHAT
Enviro pHAT by Pimonori is a small and handy addition to Raspberry Pi versions 2, 3 and 4 as well as compact Raspberry Pi Zero. It features several sensors to measure various "environmental" variables such as temperature, pressure, light level and colour, 3-axis motion, compass heading, and input voltage. Currently, this version is discontinued and Enviro (which is a pHat too, technically) is suggested as more advanced replacement.
Enviro pHAT is a HAT/pHAT board, which is connected to Raspberry Pi board via 40-pin GPIO header mount. Note that only difference between Hat and pHAT is that the latter has is designed especially to work Raspberry Pi Zero and fits its form factor. However, due to certain compatibility between the Raspberry Pi versions and revisions as well as other similar products (e.g. Orange, Banana Pi) it will work with them too. The pHat comes as a single board with separate pinout headers, which should be soldered prior to installation on Raspberry Pi board. It uses 3 GPIO pins (#2,3,4) and 5V power and communicates over i2c interface. To be mounted properly, the board should be oriented in a way so it is mounted "atop" the Raspberry Pi. To read the pHAT sensors one can use dedicated python library or dev-libs/pigpio and sys-apps/i2c-tools packages for a direct access to the GPIO pins . The following instructions are tested on Raspberry Pi 2 Model B and Enviro pHAT. Modern Enviro (not tested due to the lack of equipment) will require different python library, however, general approach would be the same.
Installation consists of the following steps:
- enabling hardware configuration in /boot/config.txt,
- checking kernel configuration and modules,
- UDEV configuration,
- software installation.
Hardware and config.txt
The pHat communicates with Raspberry Pi via I2C interface, so it should be enabled in the /boot/config.txt file, used to configure interfaces and Pi itself on the boot. Add those lines in addition to the ones present:
## --- Enviro pHAT configuration --- ## enabling required interfaces dtparam=i2c_arm=on dtparam=i2c=on dtparam=i2c_arm_baudrate=100000
Last line sets the i2c speed with 100KHz ( default frequency) which can be changed if needed.
Configuring the kernel
One needs essentially
i2c_bcmstb on RPi 4) modules for I2C communication. Raspberry Pi kernel distribution already has necessary modules pre-compiled. Alternatively, using default kernel configuration
bcm2711_defconfig for the appropriate target will select necessary modules. Otherwise check if you have these modules selected.
Device Drivers ---> I2C support ---> <M> I2C device interface I2C Hardware Bus support ---> <M> Broadcom BCM2835 I2C controller <M> BRCM Settop/DSL I2C controller
i2c_bcmstb on RPi 4) is normally loaded automatically during the boot,
i2c_dev is not.
Create a /etc/modules-load.d/envirophat.conf and add the module there:
echo "i2c_dev" >> /etc/modules-load.d/envirophat.conf
During the boot the modules the modules service will load it.
When the udev service runs it populates /dev with existing devices, including /dev/gpiomem and /dev/i2-* (normally, /dev/i2-1), which are the GPIO pins and i2c bus, which the Enviro pHat is connected to. Currently on Gentoo these devices are created with root:root ownership, however, it is more handy to change this policy so the devices are created with gpio and i2c groups, respectively, so the users can have access to them, given added to those groups.
emerge acct-group/i2c acct-group/gpio
echo 'KERNEL=="i2c-[0-9]*", GROUP="i2c", MODE="0660"' >> /etc/udev/rules.d/60-i2c-tools.rules
echo 'KERNEL=="gpiomem", GROUP="gpio", MODE="0660"' >> /etc/udev/rules.d/60-gpiomem.rules
usermod -a -G i2c gpio <your user>
If the Enviro pHat is already installed on the Raspberry Pi board at this point, one can either simply reboot, or modprobe the modules and restart the udev.
Check if the devices are present and have correct permissions.
ls /dev/i2* -al
Install the sys-apps/i2c-tools to check whether pHat is detected via i2c.
i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- -- 20: -- -- -- -- -- -- -- -- -- 29 -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- 49 -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- 77
Then, from the user added to the i2c and gpio groups in the previous section, install the required packages.
Currently (as of writing this page, Oct 2021) running just
pip install --user envirophatwill fail. The smbus package is not installed as a dependency and last stable version of RPi.GPIO fails with dev-lang/python:3.9, so alpha version should be manually installed instead.
pip install --user RPi.GPIO==0.7.1a4
pip install --user smbus
pip install --user envirophat
import envirophat works in python then, everything is configured correctly and works. E.g. create small python script file:
import envirophat print("Temperature reading is %2.2f°C" %(envirophat.weather.temperature()))
and check if the sensor reads the temperature outside the pHAT.
Temperature reading is 27.30°C
The last line is an example output, your temperature will be different!
If it worked, then everything is set up for non-root user usage.
-  Enviro pHAT in Pimonori shop.
-  Some soldering instructions.
-  Pinout of the Enviro pHat.
-  Official Raspberry Pi repository containing pre-compiled binaries of the kernel and modules and more.
-  Github repository of the official python library.
-  Official getting started page
-  Temperature Streamer/Logger example