Page 1 of 1

How to use USB Webcam on Scratch

Posted: Sat Feb 25, 2017 5:53 pm
by naohiro2g
Hi there,

Finally I found how to use my USB Webcam on Scratch in effective way for recording/playing sound and taking photos. Especially this is very nice in classroom or coder club. I have Raspberry Pi classes of 70 kids age from 9 to 13 to code to learn Scratch, Sonic Pi, Minecraft-Scratch, SunVox or others with GPIO tinkering.

You know, in this situation I have to be cost-sensitive, and to do things in easier and fun method.

Situation:
I would like to use the onboard audio jack to play sound thru, and would like to add an USB webcam, cheap and nice "UVC-compatible" or USB Video Class device to record sound, and of course, also take photos with. I have Logitech C-270 and C-310. The C-270 is cheaper and good enough.

Problem:
I always sadly see the message "Sound Recorder: unable to connect to input device " and no sound recording on Scratch.

Solution:
Survived form the deep forest of ALSA conf, I found the recipe. Only you have to do is to edit the file named ".asoundrc" at home directory or /home/pi/.asoundrc or ~/.asoundrc.

$ nano .asoundrc

Code: Select all

pcm.!default {
    type asym
    playback.pcm {
        type hw
        card 0
    }
    capture.pcm {
        type hw
        card 1
    }
}

ctl.!default {
    type hw
    card 0
}
Scratch sees only system default input and output device. You can set the onboard audio out as the default output (playback), and the mic in webcam as the default input (capture) device or "sound card" in the ALSA world with the magic words "type asym".

Hey! Now I can use the webcam as a recorder!
sound-recording-scratch.png
sound-recording-scratch.png (24.48 KiB) Viewed 2198 times
Bonus:
Taking a photo as a new costume of a sprite:
You can use the webcam to take photos in Scratch. Just go to the Costume tab then you will find the [Camera] button!
camera.jpg
camera.jpg (54.4 KiB) Viewed 2198 times
Taking photos in the script:
1. Start the GPIO server by [ broadcast ( gpioserveron ) ] block.
2. Take a photo by [ broadcast ( photo ) ] block.
GPIOserverOn.png
GPIOserverOn.png (8.18 KiB) Viewed 2198 times
3. The photo taken is stored as a new costume.

Enjoy!

naohiro

Re: How to use USB Webcam on Scratch

Posted: Sun Feb 26, 2017 12:26 am
by naohiro2g
How to use your webcam as a camera, please refer to the Raspberry Pi Org's official document "USING A STANDARD USB WEBCAM" for more details. I'm pretty sure that you should be happier by installing fswebcam package to your Raspberry Pi.
https://www.raspberrypi.org/documentati ... e/webcams/

If you can read Japanese, please visit the Japanese version at: http://qiita.com/naohiro2g/items/d87bdd1e002f881a476c

Re: How to use USB Webcam on Scratch

Posted: Sun Feb 26, 2017 3:07 am
by timrowledge
Excellent work naohiro. I tried to understand the fearsome ALSA beast once and I still get nightmares.

I'll add a reminder that 'photo' comes in several flavours for Scratch on a Pi

Basic 'photo' which produces a 240 x 180 pixel costume for a sprite or a 480x360 one for the stage
'photo big' or 'photo large' (with or without the spaces) which makes a 480x360 costumer for a sprite
'photo x@y' which tries to make a costume of size x by y. There are some values of 'x' that seem to cause problems for the v4l2 device driver; for example from my Pi camera x < 32 makes for very odd results. Some odd numbers cause trouble too. And although quite large sizes will work they may take so long to process that it seems the Pi has locked up. 'photo 2000X1600' for example *might* work but it will t a k e a w h i l e ...... <snore>