1

I wanted to make a device like microdrum. Basically, my aim is to make the RPi as a sound module or drum module. I read hits from piezo sensor and send the signal to my RPi with the help of Arduino Uno and RPi allows me to generate sound. I am using USB serial communication and Pygame for audio. The problem is when I hit the piezo, RPi generates sound but a little after the hit. My guess its 300 ms.

Here is my python code:

import serial
import pygame

pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=1024)
pygame.mixer.init()
pygame.init()
snare = pygame.mixer.Sound('snare.wav')

arduino = serial.Serial('COM23', 115200)
while True:
    data = arduino.readline()[:-2] #the last bit gets rid of the new-line chars
    if (data=='7575'):
        snare.play(loops=0, maxtime=0, fade_ms=0)
  • What piezo sensor are you using, and why not hook it up directly to the pi? – Bex Aug 16 '17 at 12:37
  • Ordinary Piezo electric sensors. Is it possible to read analog data from Piezo sensors directly to the pi? – Mehedi Hassan Rupok Aug 16 '17 at 19:00
  • Well, the Pi does not have analog input, but it doesn't seem like you are using analog input to your program anyway. Depending on the output level of your sensor you might need a transistor, but you should be able to read it digitally just to trigger an event. – Bex Aug 17 '17 at 9:02
  • You need to determine if the delay comes during the Arduino-RPi communication, or when the sound is played. For the former you can print to console instead of playing, for the latter you can periodically print to the console then play the sound. After you gained more knowledge you can make a more specific question. – ris8_allo_zen0 Aug 23 '17 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.