Analog input with usb soundcard on Linux [Part1]

I recently bought a current sensor to measure my electric house consumption. This sensor as a 1 volt amplitude output signal that can be captured by a simple sound input.

Current curve on my kettle consumption captured by the sensor. Using a 10k resistor on output

I don’t have microphone input on my raspberry pi 3. Then to use my sensor I had to buy an USB soundcard on the internet.

On my laptop (debian stretch), the usb-key is correctly seen as a sound-card :

$ sudo dmesg
[ 9474.556786] usb 1-1.2: new full-speed USB device number 5 using ehci-pci
[ 9474.668021] usb 1-1.2: New USB device found, idVendor=8086, idProduct=0808
[ 9474.668026] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 9474.668028] usb 1-1.2: Product: USB PnP Sound Device
[ 9474.668029] usb 1-1.2: Manufacturer: C-Media Electronics Inc.
[ 9474.758118] hidraw: raw HID events driver (C) Jiri Kosina
[ 9474.767742] usbcore: registered new interface driver snd-usb-audio
[ 9474.771791] usbcore: registered new interface driver usbhid
[ 9474.771793] usbhid: USB HID core driver
[ 9474.778747] input: C-Media Electronics Inc. USB PnP Sound Device as /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/1-1.2:1.3/0003:8086:0808.0001/input/input19
[ 9474.837031] hid-generic 0003:8086:0808.0001: input,hidraw0: USB HID v1.00 Device [C-Media Electronics Inc. USB PnP Sound Device] on usb-0000:00:1a.0-1.2/input3

And works same on raspberrypi3 (with raspbian) :

[171165.501265] usb 1-1.2: new full-speed USB device number 4 using dwc_otg
[171165.609589] usb 1-1.2: New USB device found, idVendor=8086, idProduct=0808
[171165.609608] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[171165.609621] usb 1-1.2: Product: USB PnP Sound Device
[171165.609634] usb 1-1.2: Manufacturer: C-Media Electronics Inc.      
[171165.618168] input: C-Media Electronics Inc.       USB PnP Sound Device as /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.3/0003:8086:0808.0001/input/input0
[171165.671634] hid-generic 0003:8086:0808.0001: input,hidraw0: USB HID v1.00 Device [C-Media Electronics Inc.       USB PnP Sound Device] on usb-3f980000.usb-1.2/input3
[171165.766026] usbcore: registered new interface driver snd-usb-audio

And seems to be correctly recognized by arecord on raspberrypi3 :

pi@raspberrypi:~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

And on my laptop !

$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC3202 Analog [ALC3202 Analog]
  Subdevices: 0/1
  Subdevice #0: subdevice #0
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0

With -L option :

$ arecord -L
default
    Playback/recording through the PulseAudio sound server
null
    Discard all samples (playback) or generate zero samples (capture)
sysdefault:CARD=PCH
    HDA Intel PCH, ALC3202 Analog
    Default Audio Device
front:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3202 Analog
    Front speakers
dmix:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3202 Analog
    Direct sample mixing device
dsnoop:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3202 Analog
    Direct sample snooping device
hw:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3202 Analog
    Direct hardware device without any conversions
plughw:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3202 Analog
    Hardware device with all software conversions
sysdefault:CARD=Device
    USB PnP Sound Device, USB Audio
    Default Audio Device
front:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    Front speakers
surround21:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    4.0 Surround output to Front and Rear speakers
surround41:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    IEC958 (S/PDIF) Digital Audio Output
dmix:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    Direct sample mixing device
dsnoop:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    Direct sample snooping device
hw:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    Direct hardware device without any conversions
plughw:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    Hardware device with all software conversions

 

To test it on my laptop I hacked an extension cord to have a wire loop and grip the current sensor. I then plugged a kettle on it to capture the electric current signal.

current loop measurement

Then I made measurement with arecord :

$ arecord -d 10 -f cd -D plughw:1 -t wav foobar.wav
Recording WAVE 'foobar.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo

And eared the sound of my boiling water with play :

[16:51:57]fabien@martonilp:~
$ play foobar.wav

foobar.wav:

File Size: 1.76M Bit Rate: 1.41M
Encoding: Signed PCM
Channels: 2 @ 16-bit
Samplerate: 44100Hz
Replaygain: off
Duration: 00:00:10.00

In:100% 00:00:10.00 [00:00:00.00] Out:441k [!=====|=====!] Hd:0.0 Clip:0
Done.

Then I can ear the sound of current boiling water :

 

In futur article we will see how to calculate the current consumption of my kettle from this wave files.

References:

[Next >> Part2]

Ce contenu a été publié dans embarqué, informatique, avec comme mot(s)-clé(s) , , , , , , , , . Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *