Example Code for Raspberry Pi-Get Data by Interrupt
Last revision 2025/12/17
This article provides a comprehensive guide on setting up a Raspberry Pi with the ENS160 Air Quality Sensor to get data through interrupts. It includes hardware and software preparation, wiring diagrams, step-by-step instructions for enabling I2C, installing dependencies, and running sample code for effective data acquisition.
Hardware Preparation
- Raspberry Pi 4 Model B - 4GB (or similar) x 1
- Fermion: NS160 Air Quality Sensor x1
- Wires
Software Preparation
Wiring Diagram

The left is connection for I2C, the right is for SPI.
Other Preparation Work
Step 1. Enable Raspberry Pi I2C.(Skip this step if it is already enabled) Open terminal and input the following commands and press "Enter":
pi@raspberrypi:~ $ sudo raspi-config
Then use the UP/Down keys to select "Interfacing Options", press Enter, select "P5 I2C" and press Enter to comfirm "Yes". Restart the Pi board.
Step 2. To install Python dependency library and git, the Raspberry Pi needs to be connected to the Internet. Skip this step if already installed. In the terminal, input the following commands and press Enter:
pi@raspberrypi:~ $ sudo apt-get update pi@raspberrypi:~ $ sudo apt-get install build-essential python-dev python-smbus git
Step 3. Download the ENS160 driver library. In the terminal, type the following commands and press Enter:
pi@raspberrypi:~ $ cd Desktop/ pi@raspberrypi:~/Desktop $ git clone https://github.com/DFRobot/DFRobot_ENS160
Sample Code
In the terminal, input the following commands and press Enter to run the code:
pi@raspberrypi:~/Desktop $ cd DFRobot_ENS160/python/raspberrypi/examples/interrupt_data_drdy
pi@raspberrypi:~/Desktop/DFRobot_ENS160/python/raspberrypi/examples/interrupt_data_drdy $python interrupt_data_drdy.py
Result

Please let the sensor run for 1 hour first to ensure the accuracy of the data when using it for the first time, and then perheat for 3 minute each time it is used. You also need to set the current ambient temperature and humidity to assist in detecting result.
Was this article helpful?
