Example Code for Pyboard-Analog Sound Sensor

Last revision 2026/01/05

This article guides you through setting up and coding an analog sound sensor with Pyboard V1.1, using the Gravity I/O Expansion Shield, uPyCraft IDE, and PuTTY software. It includes detailed hardware preparation, wiring instructions, sample code, and expected results, providing a comprehensive resource for measuring sound levels effectively with Pyboard.

Hardware Preparation

  • pyboard V1.1 x1
  • Gravity: I/O Expansion Shield for Pyboard x1
  • Gravity 3pin sensor Cable (or several DuPont Cables) x1
  • MicroUSB Cable x1
  • Gravity: Analog Sound Sensor x1

Software Preparation

Wiring Diagram

As shown below,connect the sensor to the X5 port of the expansion shield. Pyboard_sound_wire.jpg

Sample Code

# Analog sound sensor is used to measure the sound.
# Hardware : analog sound sensor, pyboard v1.1
# connect:
#     sensor    pyboard
#     VCC       3V3
#     GND       GND
#     data      X5
from pyb import ADC,Pin
import time
adc=ADC(Pin('X5'))   # Connect sensor to 'X5'
while True:
  val=adc.read()     # Reed the analog value
  print(val)
  time.sleep(0.1)

Result

Copy the sample code in the uPyCraft IDE, save it, then click the DownloadAndRun button, you will see the value of the sound.
As shown in the picture below, when there is sound, the value will increase significantly.
Pyboard_sound.jpg

Was this article helpful?

TOP