Example Code for OpenMV-Analog Sound Sensor
How to set up and use an OpenMV Cam M7 with a Gravity Analog Sound Sensor for sound measurement. It covers hardware and software preparations, wiring instructions, and provides sample code to read sound levels using Python, showing how values change with sound fluctuations.
Hardware Preparation
- OpenMV Cam M7 – A Machine Vision Camera x1
- Gravity: I/O Expansion Shield for OpenMV Cam M7 x1
- Gravity 3pin sensor Cable (or several DuPont Cables) x1
- MicroUSB Cable x2
- Gravity: Analog Sound Sensor x1
Software Preparation
- OpenMV IDE, Click to Download OpenMV IDE
Wiring Diagram
As shown below,connect the sensor to the P6 port of the expansion shield.

Other Preparation Work
When put the expansion shield on the OpenMV motherboard, please pay attention to the direction. The USB port of the expansion shield should be on the same side as the USB port of the OpenMV motherboard.
Sample Code
# Analog sound sensor is used to measure the sound.
# Hardware : analog sound sensor, OpenMV
# connect:
# Sensor OpenMV
# VCC 3V3
# GND GND
# data P6
from pyb import ADC,Pin
import time
adc=ADC(Pin('P6')) # Must always be "P6".
while True:
val=adc.read() # Read the analog value
print(val)
time.sleep(100)
Result
Copy the sample code in the OpenMV IDE and click the start button. As shown in the picture below, when there is sound, the value will increase significantly.

Was this article helpful?
