Last Updated: 2019-11-10
This work is a collaborative project between Cardiff University and Bridgend College. We are grateful to the Wellcome Trust for their financial contributions to the project and to Llanishen Fach Primary School for allowing us to trial and test the system.
Students form a circle or a line and each one is given a piece of A4 paper with a line of Python code on it.
Within the code, there will be a spelling error, a variable definition, an if statement and a loop. The tutor should discuss these various programming constructs as the students move around the circle.
print('Boom!')
jump()
prin('Test') #Note deliberate spelling error!
if <next person is wearing glasses>:
for n in range(1,3):
Downloads:
During this section of the class, students will assemble their Raspberry Pis and login. There are some excellent ‘Getting Started' resources provided by the Raspberry Pi foundation which can be found below:
You might want to try out some physical computing activities first, to get your students familiar with the concepts of wiring a breadboard, a button, an LED and so on. You can find some very thorough tutorials below, for both Scratch (beginner) and Python (more advanced).
Hardware resources:
Before getting started with the camera module, we will have to make sure it is enabled via the config page. To do this, you'll need to open a Raspberry Pi terminal. You can do that by following the instructions here.
sudo raspi-config
Interfacing Options
and select Camera.
Select the option to enable the camera and reboot the Pi.Once the camera module is connected, you may want to follow some of the recommended instructions from the Raspberry Pi foundation, available below:
The code generally reduces to the following two commands:
raspistill -o picture.jpg
and
raspivid -o video.h264 -t 10000
The first command takes a picture and the second takes a 10 second (10000 milisecond) video and stores them on the Pi.
To record audio with the Pi, you can use the following command.
arecord -D plughw:1,0 -d 3 audio.wav
This records 3 seconds of audio and stores it to the Pi. It is recommended to follow the instructions below to check each part of this command is correct for your system.
mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
mmal: camera component could not be enabled
mmal: main: Failed to create camera component
mmal: Failed to run camera app. Please check for firmware updates
If this happens, first check that the camera module is enabled with step 1 and reboot the Pi.
It is possible that the camera connection wire is faulty if this error persists. Unplug the camera module, ensure it is in the correct way around and try with a different camera module or Raspberry Pi.
There is further information below about troubleshooting this error:
Hardware resources:
So far, we have assembled the hardware and tested the action of the button using Scatch (or Python) to light an LED and tested our camera module with audio. This section looks to bring all of this together into one command that can be run outside at the hive.
We need to do a few things to make this possible.
if the button is pressed, start the recording
' . This will embed many of the concepts from the Coding Circle activity. Following on from the Building the Hardware section, we can use Python to trigger the camera to take a photo when a button is pressed. This is covered by the following code:
from gpiozero import Button
from picamera import PiCamera
from datetime import datetime
from signal import pause
import os #required for audio stream
button = Button(2)
camera = PiCamera()
def capture():
timestamp = datetime.now().isoformat()
camera.capture('/home/pi/%s.jpg' % timestamp)
button.when_pressed = capture
pause()
This code will store the output file as a jpg in the /home/pi/
directory on the Pi. Note that we are labelling the image as the date and time to avoid overwriting, as discussed in Building the Hardware.
Capturing a video to a file is similar and can be done in Python as follows:
from gpiozero import Button
from picamera import PiCamera
from datetime import datetime
from signal import pause
import os #required for audio stream
button = Button(2)
camera = PiCamera()
seconds = 60 #you can change this number
def capture():
timestamp = datetime.now().isoformat()
camera.resolution = (640, 480)
camera.start_recording('/home/pi/%s.h264' % timestamp)
camera.wait_recording(seconds)
camera.stop_recording()
button.when_pressed = capture
pause()
This captures 60 seconds of footage and stores it in the /home/pi/
directory on the Pi.
You can add the audio stream by adding a line as follows:
from gpiozero import Button
from picamera import PiCamera
from datetime import datetime
from signal import pause
import os #required for audio stream
button = Button(2)
camera = PiCamera()
seconds = 60 #if you change this, change the 60 in the audio command
def capture():
timestamp = datetime.now().isoformat()
camera.resolution = (640, 480)
camera.start_recording('/home/pi/%s.h264' % timestamp)
camera.wait_recording(seconds)
os.system('arecord -D plughw:1,0 -d 60 audio.wav') #for audio
camera.stop_recording()
button.when_pressed = capture
pause()
There are multiple ways to set your Python file to run when the Raspberry Pi loads up. Firstly, make sure you have the code from Step 1 saved on your Pi somewhere that you can find it. It is probably sensible to name it something like startVideo.py
and store it in your /home/pi/
directory.
The following tutorial explains 5 different ways to get your program to run at boot (start up):
From experience, sometimes some of them don't work, so make sure to test it and use a different method if necessary.
This may be one of the trickiest parts of this whole process, and Raspberry Pi don't really provide an easy way to do this (at least at the time of writing this tutorial). It requires quite a lengthy command that needs to be typed into a terminal.
It is up to you whether you want the students to type this, or simply copy and paste. At a basic level, it pipes the output of the command raspivid
to arecord
and combines them together. Here's the code:
/usr/bin/raspivid -o - -t 0 -w 1280 -h 720 -vf -fps 40 -b 8000000 -g 40 -n | /usr/bin/ffmpeg -ac 1 -f alsa -ac 1 -i hw:1,0 -f h264 -i pipe:0 -c:v copy -c:a aac -ab 128k -g 40 -f mp4 -r 30 video.mp4
ffmpeg
(only if not installed):cd ~
git clone git://source.ffmpeg.org/ffmpeg --depth=1
cd ffmpeg
./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree
make -j4
sudo make install
The following code should be saved and stored in the /home/pi/
directory.
from gpiozero import Button
from signal import pause
import os #required for audio stream
button = Button(2)
def capture():
os.system('/usr/bin/raspivid -o - -t 0 -w 1280 -h 720 -vf -fps 40 -b 8000000 -g 40 -n | /usr/bin/ffmpeg -ac 1 -f alsa -ac 1 -i hw:1,0 -f h264 -i pipe:0 -c:v copy -c:a aac -ab 128k -g 40 -f mp4 -r 30 video.mp4') #for audio
button.when_pressed = capture
pause()
Use Step 2 to enable this script to run at boot.
Congratulations, you have reached the end of the tutorial. Good luck!
If you have any questions, please contact Scott Morgan | smorgan@bridgend.ac.uk.
Resources & Further Reading: