Example Code for Raspberry Pi – Analog Sound Sensor
Last revision 2026/01/20
This article offers a guide on integrating and coding an analog sound sensor with Raspberry Pi, including setup instructions and example code for sound monitoring using Python.
Introduction
This is a simple and affordable microphone through which the Arduino can sense the level of the sound and convert it into an analog signal. That is, the volume is reflected by the feedback voltage value.
The analog data line directly corresponds to the analog port on the IO expansion board V7. Plug in and burn the code, you can use it.

The sensor reads O when completely muted. When there is music nearby, it will read various readings with the volume.

Precautions
The layout of the new version of the analog sensor port has the following two improvements. Please refer to our blog "How to change the data line connector layout" instructions. When using the sensor on the IO expansion board, you may need to adjust the layout of the connector. For your convenience, we will make more improvements, so stay tuned.

Wiring Diagram
- Power the Raspberry Pi on and install the Raspberry Pi expansion board correctly
- Connect the sensor to analog port 0 on the expansion board

Sample Code
-
Install Python dependency libraries and git, and you need to get your Raspberry Pi connect to internet for this step(skip if installed). In the terminal, type the following instructions and press ‘Enter’
sudo apt-get install build-essential python-dev python-smbus git -
Download the drive library and program. In the terminal, type the following instructions and press ‘Enter’
cd \~
git clone https://github.com/DFRobotdl/37\_Pcs\_Sensor.git
- Find Analog_Sound_Sensor.py in \37_Pcs_Sensor\11_Analog_Sound_Sensor, open and run it with Thonny Python IDE, you can see the printed sound values. The smaller value, the louder volume. And vice versa.


Was this article helpful?
