Upload
3582
View
226
Download
0
Embed Size (px)
Citation preview
7/28/2019 The MagPi 2012 12 Issue 8
1/34
MMeerrrryyCChhrriissttmmaassFFrroommTThheeMMaaggPPii
IISSSSUUEE 0088 -- DDEECC 22001122
AAMMaaggaazziinneeffoorrRRaassppbbeerrrryyPPiiUUsseerrss
hh tt tt pp ::////ww ww ww .. tt hh ee mm aa gg pp ii .. cc oo mm
Raspberry Pi is a trademark of The Raspberry Pi Foundation.This magazine was created using a Raspberry Pi computer.
TThhiiss IIssssuuee......
SSkkuutttteerr
NNaannppyy
PPii GGaauuggee
PPiibbooww CCEESSIILL PPii
CC++++
AAddaa
MMyySSQQLL
PPyytthhoonn PPiitt
CCaattcchh SSaannttaa uussiinngg
hhoommee aauuttoommaattiioonnWin a 512MBRaspberry Pi
Visit our Kickstarter
http://kck.st/TvkdvG
for printed MagPi!
7/28/2019 The MagPi 2012 12 Issue 8
2/34
Welcome to the eighth edition of the MagPi magazine,
Its Christmas! In this issue we hope to entice you into some festive projects to try after gorging yourself to the brim with
Christmas pudding.
In this months edition, we introduce you to a simple home automation project allowing you to control lights and
appliances in your house using the power of the Pi! Just in time to catch Mr Claus! We get your Skutter project in motion
with Morphys article on adding wheels to your base. Gordon teaches us how to light up a Xmas tree, we have more on
using the Pi to control an Arduino and Ben describes how to control servos attached to the Pi using the Internet! If this
isnt enough we have more of the old favourites plus an introduction to SQL.
As per always, we have some great gifts for you to win in our monthly magazine. The MagPi would like to say a big
thank you yet again to PC Supplies who this month has outdone themselves by offering up a 512MB Raspberry Pi for
grabs!
In addition to this we have some exciting news for you this month. As of December 1st, we at the MagPi, are so excited
to be able to offer our readers the possibility of a printed version of all eight issues of the magazine! This is something
which gets constantly requested of us from our readers. All eight issues will be beautifully wrapped up in limited edition
MagPi binder making it a great gift to yourself or any of your loved ones of any age. For more information on this please
visit www.kickstarter.com/projects/themagpi/the-magpi-magazine-from-virtual-to-reality
On behalf of the whole team, thank you again for all your support. We hope you have a fantastic Christmas and we will
see you in the New Year (1st of February). While we have not managed to squeeze it into this issue, you might be
interested in www.xmas4all.co.uk from which you will be able to control their Raspberry Pi powered Christmas lights!
Ash Stone
Chief Editor of The MagPi
M a g P i t e a m
A s h S t o n e - C h i e f E d i t o r / A d m i n i s t r a t o r
C h r i s ' t z j ' S t a g g - W r i t e r / P h o t o g r a p h e r / P a g e D e s i g n s
C o l i n D e a d y - W r i t e r / P a g e D e s i g n s
J a s o n ' J a s e m a n ' D a v i e s - W e b s i t e / P a g e D e s i g n s
M a t t ' 0 t h e 0 j u d g e 0 ' - W e b s i t e / A d m i n i s t r a t o r
M e l t w a t e r - P h o t o g r a p h e r / P a g e D e s i g n s / A d m i n .
A a r o n S h a w - P a g e D e s i g n s / G r a p h i c s
I a n M c A l p i n e - P a g e D e s i g n s / G r a p h i c s
L i x - P a g e D e s i g n s / G r a p h i c s
S a m M a r s h a l l - P a g e D e s i g n s / G r a p h i c s
W . H . B e l l - P a g e D e s i g n s
G u e s t w r i t e r s
B o d g e N H a c k i t t - W r i t e r
G e o f f J o h n s o n - W r i t e r
A n d r e a S t a g i - W r i t e r
B e n S c h a e f e r - W r i t e r
G o r d o n H e n d e r s o n - W r i t e r
Al e x K e r r - W r i t e r
L u k e G u e s t - W r i t e r
R i c h a r d W e n n e r - W r i t e r
2
7/28/2019 The MagPi 2012 12 Issue 8
3/34
0 4 S K U T T E R R E T U R N S
Dig out the toolbox for the next thrilling installment, by Bodge N Hackitt
0 8 H O M E A U T O M A T I O N - S A N T A T R A P
Control your home with a Raspberry Pi and catch Santa in the act! by Geoff Johnson
1 1 T H I S M O N T H ' S C O M P E T I T I O N
Win a 512Mb Raspberry Pi Model B, from PC Supplies UK
1 2 C O N T R O L Y O U R A R D U I N O W I T H P Y T H O N & R A S P B E R R Y P I
The power of Raspberry and the simplicity of Arduino using Nanpy, by Andrea Stagi
1 4 P I G A U G E
Control servos over the internet, by Ben Schaefer
1 7 B O O K P R O M O T I O N - G E T T I N G S T A R T E D W I T H P Y T H O N
Simon Monk's new book covering basic to full GPIO python examples.
1 8 P I B O W I N T E R V I E W
An interview with the designers of the PiBow case, by Chris Stagg
2 0 C E S I L P O W E R E D C H R I S T M A S T R E E
Christmas from the 70's using the CESIL programming language, by Gordon Henderson
2 2 W E L C O M E T O T H E C + + C A C H E
Using basic variables and STL strings, by Alex Kerr
2 4 B E G I N I N G A D A
The second installment in our Ada programming tutorial, by Luke A. Guest
2 6 D A T A B A S E B O O T C A M P
Get your teeth into some Structured Query Language (SQL), by Richard Wenner
2 9 T H I S M O N T H ' S E V E N T S L I S T
Raspberry Jams and other community events
3 0 T H E P Y T H O N P I T
Creating multiple desktop widgets, by Colin Deady
3 2 T H E Y E A R O F T H E M A G P I
Contents
3
7/28/2019 The MagPi 2012 12 Issue 8
4/34
A s i m p l e s w i t c h i n g H b r i d g e
I n t h e l a s t a r t i c l e w e l o o k e d a t s o m e p h y s i c a l
m e a n s o f a d d i n g m o t o r s t o a r o b o t a n d
i n v e s t i g a t e d a d a p t i n g s o m e m o t o r i s e d
e l e c t r o n i c t o y s a s a p o t e n t i a l s o u r c e f o r r o b o t
b a s e s .
I n t h i s a r t i c l e I w i l l b e g i n t o e x p l a i n h o w y o u
c a n b u i l d y o u r v e r y o w n D C e l e c t r o n i c m o t o r
d r i v e r m o d u l e a n d w r i t e a b a s i c c o n t r o l
p r o g r a m f o r i t .
W e w i l l s t a r t b y r e - e x a m i n i n g t h e s t a n d a r d D C
m o t o r t h a t w a s c o v e r e d i n t h e p r e v i o u s a r t i c l e .
T o m a k e t h e m o t o r r u n f o r w a r d s w e a p p l y a
p o w e r s o u r c e b e t w e e n t h e + a n d t e r m i n a l s
o n t h e m o t o r a n d t o m a k e i t r u n i n r e v e r s e ,
s i m p l y s w a p t h e p o w e r s o u r c e t e r m i n a l s
a r o u n d .
T h e m o t o r d r i v e r m o d u l e w e a r e g o i n g t o
c r e a t e w i l l n e e d t o b e a c i r c u i t w h i c h i s a b l e t o
d o t h i s s w a p p i n g a r o u n d o f t h e p o w e r s u p p l y
t e r m i n a l s e l e c t r o n i c a l l y . T h i s c a n b e
a c c o m p l i s h e d u s i n g a H b r i d g e c i r c u i t .
T h i s d i a g r a m s h o w s a s i m p l i f i e d v e r s i o n o f
s u c h a c i r c u i t . C l o s i n g s w i t c h e s 1 a n d 2
e f f e c t i v e l y c o n n e c t s t h e p o s i t i v e r a i l o f t h e
p o w e r s u p p l y t o t h e + t e r m i n a l o n t h e m o t o r
a n d g r o u n d t o t h e t e r m i n a l a n d t h e m o t o r
r u n s f o r w a r d s . A l t e r n a t i v e l y , c l o s i n g s w i t c h e s
3 a n d 4 c o n n e c t s t h e g r o u n d t o t h e + t e r m i n a l
a n d t h e p o s i t i v e r a i l t o t h e t e r m i n a l a n d t h e
m o t o r r u n s i n r e v e r s e .
T h e r e i s a p o t e n t i a l l y d a n g e r o u s s i t u a t i o n i f
s w i t c h e s 1 a n d 4 o r 3 a n d 2 a r e c l o s e d . T h i s
w o u l d c r e a t e a s h o r t c i r c u i t b e t w e e n P o w e r
S u p p l y + a n d G r o u n d w h i c h c a n b e v e r y
p r o b l e m a t i c t o s a y t h e l e a s t . C a r e m u s t b e
t a k e n w h e n c o n t r o l l i n g t h i s c i r c u i t t o e n s u r e
t h a t t h i s s i t u a t i o n c a n n e v e r h a p p e n .
I n r e a l i t y w e c a n t h a v e f o u r p h y s i c a l o n / o f f
s w i t c h e s l i k e t h i s a s w e n e e d t o c o n t r o l t h e
c i r c u i t u s i n g t h e G P I O o n t h e R a s p b e r r y P i .
T h e r e a r e e l e c t r o n i c s o l u t i o n s t o t h i s . O n e
p o s s i b i l i t y i s t h e u s e o f e l e c t r o m a g n e t i c r e l a y s
t o c l o s e t h e s e s w i t c h e s , h o w e v e r t h e
R a s p b e r r y P i i s n o t a b l e t o d e l i v e r e n o u g h
p o w e r f r o m t h e G P I O t o d i r e c t l y a c t i v a t e s u c h
a r e l a y w i t h o u t h a v i n g s o m e t h i n g i n - b e t w e e n
s u c h a s a t r a n s i s t o r . T h i s l e a d s u s t o t h e
s e c o n d p o s s i b l e s o l u t i o n w h i c h i s t o s i m p l y
u s e s o m e t r a n s i s t o r s a s s w i t c h e s .
T r a n s i s t o r s a s s w i t c h e s
T h e t r a n s i s t o r i s a r g u a b l y t h e m o s t i m p o r t a n t
e l e c t r o n i c i n v e n t i o n e v e r c r e a t e d . I t s
d e v e l o p m e n t i s r e s p o n s i b l e f o r e v e r y t h i n g
f r o m p o r t a b l e m u s i c p l a y e r s t o t h e p r o c e s s o r
u s e d i n t h e R a s p b e r r y P i .
D I F F I C U L T Y : A D V A N C E D Part 2
Adding a motorised base
4
7/28/2019 The MagPi 2012 12 Issue 8
5/34
W e w i l l b e l o o k i n g a t N P N t y p e t r a n s i s t o r s .
T h i s d e v i c e h a s t h r e e t e r m i n a l s c a l l e d b a s e ,
c o l l e c t o r a n d e m i t t e r .
C o n n e c t i n g a p o w e r s u p p l y a c r o s s t h e
c o l l e c t o r a n d e m i t t e r a l l o w s t h e t r a n s i s t o r t o b e
u s e d a s a s w i t c h . W i t h o u t a c o n n e c t i o n t o t h e
b a s e , t h e i n t e r n a l r e s i s t a n c e o f t h e t r a n s i s t o r
i s e x t r e m e l y h i g h a n d t h e s w i t c h i s o f f .
I f w e a p p l y a c u r r e n t t o t h e t r a n s i s t o r b a s e
t h e n t h e i n t e r n a l r e s i s t a n c e w i l l d r o p b y a
c o r r e s p o n d i n g a m o u n t a n d m o r e c u r r e n t w i l l
f l o w f r o m t h e c o l l e c t o r t o t h e e m i t t e r .
T h e t r a n s i s t o r i s a b l e t o v a r y i t s i n t e r n a l
r e s i s t a n c e v e r y q u i c k l y , t e n s o f t h o u s a n d s o f
t i m e s p e r s e c o n d . ( I t s t h i s f e a t u r e t h a t a l l o w s
t r a n s i s t o r s t o b e u s e d a s a m p l i f i e r s ) .
T h e a m o u n t t h a t t h e c u r r e n t a f f e c t s t h e
i n t e r n a l r e s i s t a n c e o f t h e t r a n s i s t o r i s d e f i n e d
b y a r a t i o k n o w n a s t h e D C c u r r e n t g a i n a n d
i s r e f e r r e d t o a s h
F E
.
I n o u r c a s e w e w a n t t o s u p p l y a c u r r e n t t o t h e
b a s e t h a t w i l l m a k e t h e i n t e r n a l r e s i s t a n c e b e
n e a r z e r o j u s t l i k e a c l o s e d s w i t c h . T h i s i s
c a l l e d T r a n s i s t o r S a t u r a t i o n a n d t h e r e i s a n
e q u a t i o n w h i c h t e l l s u s t h e c u r r e n t w e n e e d t o
a p p l y t o t h e b a s e t o m a k e t h i s h a p p e n ,
IB = IC / hFE
w h e r e I
C
i s t h e c o l l e c t o r c u r r e n t a n d I
B
i s t h e
b a s e c u r r e n t . I n o r d e r t o f i n d o u t w h a t t h i s
c u r r e n t i s i t s n e c e s s a r y t o m e a s u r e t h e
c u r r e n t t h a t s d r a w n b y t h e m o t o r . T h i s m e a n s
a n e x p e r i m e n t i s n e e d e d !
F o r t h i s y o u w i l l n e e d y o u r m o t o r i s e d b a s e ( i n
m y c a s e i t s t h e m o d i f i e d B i g T r a k ) , a p o w e r
s u p p l y ( s o m e b a t t e r i e s ) a n d a m u l t i m e t e r .
I f y o u d o n t o w n a m u l t i m e t e r y e t , t h e y a r e a n
e s s e n t i a l t o o l f o r a n y o n e w h o i s i n v o l v e d i n
e l e c t r o n i c s a n d a l l o w y o u t a k e a w i d e r a n g e o f
m e a s u r e m e n t s i n c l u d i n g v o l t a g e , c u r r e n t ,
r e s i s t a n c e , c a p a c i t a n c e a n d h
F E
. M a p l i n s s e l l
o n e f o r 7 . 9 9 ( C O D E : N 2 0 A X ) .
I t i s p o s s i b l e t o o b t a i n a g o o d m u l t i m e t e r f o r
u n d e r t e n p o u n d s f r o m a v a r i e t y o f r e t a i l e r s .
D C m o t o r s d r a w d i f f e r e n t c u r r e n t s u n d e r
d i f f e r e n t c o n d i t i o n s . I f a m o t o r i s f r e e -
w h e e l i n g t h e n t h e m o t o r w i l l d r a w a
c o m p a r a t i v e l y s m a l l c u r r e n t .
A l t e r n a t i v e l y a s t a l l e d m o t o r ( a m o t o r t h a t i s
p r e v e n t e d f r o m t u r n i n g ) w i l l d r a w a n e x t r e m e l y
h i g h c u r r e n t . T h e h a r d e r w e m a k e a m o t o r
w o r k , t h e m o r e c u r r e n t i t w i l l d r a w . I n o u r c a s e
w e w a n t t o m e a s u r e t h e c u r r e n t t h e m o t o r s
d r a w w h e n o u r r o b o t i c b a s e i s t r u n d l i n g a l o n g
t h e f l o o r . O n e w a y t o a c c u r a t e l y o b t a i n t h i s
m e a s u r e m e n t i s t o m a k e t h e b a s e m o v e o n
t h e g r o u n d a n d m e a s u r e t h e c u r r e n t t h a t i s
b e i n g d r a w n . H e r e i s t h e m e t h o d I u s e d w i t h
m y B i g T r a k :
C o n n e c t t h e m u l t i m e t e r i n s e r i e s b e t w e e n t h e
b a t t e r y / p o w e r s u p p l y a n d o n e o f t h e m o t o r s i n
t h e B i g T r a k .
T h e s e c o n d m o t o r m u s t a l s o b e c o n n e c t e d t o
t h e p o w e r s u p p l y a n d a c t i v e o t h e r w i s e o n l y
o n e m o t o r w i l l t r y t o d r i v e t h e w h o l e B i g T r a k
w h i c h w i l l r e s u l t i n a n i n a c c u r a t e
m e a s u r e m e n t . H o w e v e r w e o n l y n e e d t o
m e a s u r e t h e c u r r e n t d r a w n b y o n e o f t h e s e
t w o i d e n t i c a l m o t o r s .
A d d s o m e w e i g h t t o t h e B i g T r a k w h i c h
a p p r o x i m a t e s t h e e x p e c t e d o v e r a l l w e i g h t o f
t h e f i n i s h e d r o b o t . I n t h e c a s e o f t h e S k u t t e r
t h i s i n c l u d e s a d d i n g t h e r o b o t a r m .
C o m p l e t e t h e c i r c u i t b e t w e e n t h e b a t t e r i e s a n d
m o t o r , i n c l u d i n g t h e m u l t i m e t e r i n s e r i e s a s
s h o w n . A s t h e B i g T r a k r o l l s a l o n g t h e f l o o r ,
t a k e a m e a s u r e m e n t o f t h e c u r r e n t w h i c h i s
b e i n g d r a w n . U n d e r t h e e x p e c t e d l o a d f o r t h e
S k u t t e r u s i n g t h i s m e t h o d , o n e o f t h e t w o b i g
t r a k m o t o r s w i l l d r a w a c u r r e n t o f 2 . 5 A m p s .
C A U T I O N : W h e n m o t o r s t a l l i n g w a s t e s t e d
t h e c u r r e n t d r a w n w a s a p p r o x i m a t e l y 2 0
A m p s .
C o n t i n u e d o v e r p a g e . . .
5
7/28/2019 The MagPi 2012 12 Issue 8
6/34
C h o o s i n g t h e r i g h t t r a n s i s t o r
U s i n g t h e r i g h t t r a n s i s t o r c a n m a k e a m a s s i v e
d i f f e r e n c e t o t h e d e s i g n o f y o u r c i r c u i t . O f t e n
s e v e r a l f a c t o r s n e e d t o b e c o n s i d e r e d
s i m u l t a n e o u s l y . F i r s t , w e n e e d a n i d e a o f t h e
m i n i m u m g a i n w e n e e d t o p r o v i d e . W e w i l l
u s e t h e s a t u r a t i o n e q u a t i o n g i v e n b e f o r e ,
h o w e v e r i t i s c o m m o n t o a l l o w f o r 5 t i m e s t h e
h
F E
v a l u e t o p r o v i d e a n o p e r a t i n g m a r g i n ( i f
y o u l o o k c l o s e l y a t t r a n s i s t o r d a t a s h e e t s t h i s
v a l u e c a n v a r y w i d e l y d u e t o h o w h a r d i t i s
d r i v e n ) . H e r e , I
C
i s t h e c u r r e n t w e n e e d f o r o u r
m o t o r a t 2 . 5 A a n d I
B
i s t h e c u r r e n t w e c a n
s u p p l y t o s w i t c h t h e t r a n s i s t o r ( R P i G P I O p i n
c a n s u p p l y u p t o 1 5 m A , 5 1 m A m a x i m u m i n
t o t a l ) .
hFE(min) > 5 x ( IC/IB)
= 5 x (2.5/0.015) = 833
( t y p i c a l t r a n s i s t o r s g a i n s a r e n e a r e r 1 0 0 )
T h e n e x t c o n s i d e r a t i o n i s t h e t r a n s i s t o r w i l l
n e e d t o h a n d l e 2 . 5 A t h r o u g h t h e c o l l e c t o r a n d
e m i t t e r , s o I
C E ( m a x )
n e e d s t o b e h i g h e n o u g h
( a n d a V
C E ( m a x )
h i g h e n o u g h f o r y o u r m o t o r
s u p p l y . F i n a l l y , t h e v o l t a g e d r o p V
C E ( s a t )
i s a l s o
i m p o r t a n t , s i n c e n o t o n l y d o e s t h i s d e t e r m i n e
h o w m u c h o f t h e s u p p l y v o l t a g e m a k e s i t
t h r o u g h t o t h e m o t o r , b u t t h e a m o u n t o f p o w e r
w a s t e d ( a s h e a t - t o o h o t = m e l t e d ) b y t h e
t r a n s i s t o r ( t h i s i s g i v e n b y P o w e r = I
C
x
V
C E ( s a t )
) .
T h e a l a r m i n g g a i n v a l u e c a n b e s o l v e d b y
u s i n g m o r e t h a n o n e t r a n s i s t o r i n s e r i e s , s o
t h e G P I O s i g n a l i s a m p l i f i e d i n s t a g e s . O n e
w a y t o d o t h i s i s t o u s e a s p e c i a l a r r a n g e m e n t
c a l l e d a D a r l i n g t o n P a i r , t h e s e a r e o f t e n
a v a i l a b l e w r a p p e d u p i n a s i n g l e p a c k a g e . I n
f a c t , T I P 1 2 0 , T I P 1 2 1 a n d T I P 1 2 2 a r e
d e s i g n e d f o r s u c h a p p l i c a t i o n s , o f f e r i n g h
F E
v a l u e s a r o u n d 1 0 0 0 , I
C ( m a x )
o f 5 A m p s a n d i n a
s u i t a b l e f o r m t o a t t a c h a l a r g e h e a t s i n k i f
r e q u i r e d .
H o w e v e r , a n o t h e r o p t i o n i s t o m a k e u s e o f
e x t r a t r a n s i s t o r s t o p e r f o r m t h e s w i t c h i n g o f
t h e H - B r i d g e a n d a l s o p r o v i d e i s o l a t i o n f r o m
t h e m o t o r s u p p l y v o l t a g e s ( a n y t h i n g a b o v e
3 . 3 V o n t h e G P I O i s b a d n e w s ) . T h e g a i n
f r o m t h e s e t r a n s i s t o r s c a n b e u s e d t o r e d u c e
t h e r e q u i r e d g a i n o f t h e H - B r i d g e t r a n s i s t o r s ;
w i t h s o m e c a r e f u l s e l e c t i o n i t m a y b e p o s s i b l e
t o d r i v e t h e m o t o r w i t h o u t t h e D a r l i n g t o n P a i r s
( s e e h t t p : / / g o o . g l / g g H r q ) . T h i s l e a d s t o t h e
f i n a l c o n s i d e r a t i o n , w h a t e v e r t r a n s i s t o r s y o u
d e s i g n f o r y o u l l n e e d t o s o u r c e t h e m o r
e q u i v a l e n t o n e s , h e n c e w h y I v e s e t t l e d o n t h e
T I P d e v i c e s . T h e t r a d e - o f f i s t h a t t h e V
B E ( s a t )
c a n r a n g e f r o m 0 . 7 V 4 V , d r o p p i n g a l a r g e
c h u n k o f t h e s u p p l y v o l t a g e , d e p e n d i n g o n t h e
r a t i o o f I
B
a n d I
C
.
A T r a n s i s t o r M o t o r D r i v e r C i r c u i t
B e l o w i s a " W e i s s " H - B r i d g e c i r c u i t , y o u w i l l
s e e t h a t o u r s i m p l e s w i t c h d e s i g n i s a l i t t l e
m o r e c o m p l e x t h a n w e t h o u g h t , b u t e a c h p a r t
o f t h e c i r c u i t p e r f o r m s a n i m p o r t a n t j o b .
T h e r e a r e f o u r d i o d e s ( D 1 - D 4 ) w h i c h a r e
o r i e n t a t e d i n t h e o p p o s i t e d i r e c t i o n t o t h e f l o w
o f c u r r e n t . T h i s i s b e c a u s e a D C m o t o r c a n
a l s o g e n e r a t e a n e l e c t r i c c u r r e n t w h e n i t t u r n s
f a s t e r t h a n w e a r e d r i v i n g i t . T h i s i s r e f e r r e d t o
a s f l y - b a c k c u r r e n t a n d i t c a n b e h i g h
e n o u g h t o d a m a g e t h e t r a n s i s t o r s . H a v i n g
t h e s e r e v e r s e b i a s e d d i o d e s a l l o w s a n y " f l y -
b a c k " c u r r e n t t o e s c a p e s a f e l y . I f y o u h a v e
l o o k e d a t t h e d a t a s h e e t s f o r t h e T I P d e v i c e s
y o u w i l l n o t i c e t h e y a l r e a d y i n c l u d e t h e d i o d e s
i n t e r n a l l y , s o y o u w o n ' t n e e d t o a d d e x t r a o n e s
( b u t i t i s i m p o r t a n t t o r e m e m b e r i f u s i n g o t h e r
d e v i c e s ) .
A l s o , y o u m a y n o t e t h e t o p t r a n s i s t o r s a r e
s l i g h t l y d i f f e r e n t t o t h e b o t t o m ( t h e a r r o w s
p o i n t i n w a r d s ) , t h e s e a r e P N P t y p e d e v i c e s
r a t h e r t h a n N P N . T h i s a l l o w s t h e d r i v i n g
v o l t a g e o f t h e m o t o r t o b e g r e a t e r t h a n t h e
G P I O v o l t a g e . T h e P N P " t w i n s " o f t h e N P N
d e v i c e s m e n t i o n e d a r e T I P 1 2 5 , T I P 1 2 6 a n d
T I P 1 2 7 . T h e y f u n c t i o n t h e s a m e , e x c e p t
i m p o r t a n t l y , t h e y a r e a c t i v e L O W , s o t h e G P I O
h a s t o b e t u r n e d O F F t o s w i t c h t h e t r a n s i s t o r
O N .
Y o u w i l l n o t i c e t h a t t w o e x t r a t r a n s i s t o r s a r e
u s e d t o s w i t c h o n a n d o f f t h e T I P t r a n s i s t o r s ,
t h i s a l l o w s t h e m o t o r s u p p l y t o b e g r e a t e r t h a n
t h e G P I O v o l t a g e w i t h o u t p u t t i n g t h a t v o l t a g e
o n t h e G P I O p i n ( w i t h b a d r e s u l t s f o r t h e R P i ) .
A n i c e s i d e - e f f e c t i s t h a t n o t o n l y d o e s t h i s
m a k e c o n t r o l l i n g t h e P N P d e v i c e s e a s y , b u t
6
7/28/2019 The MagPi 2012 12 Issue 8
7/34
b o t h t h e t o p a n d b o t t o m " s w i t c h e s " c a n n o w
b e c o n t r o l l e d w i t h 1 G P I O p i n f o r e a c h
d i r e c t i o n .
S e l e c t i o n o f t h i s t r a n s i s t o r i s a l i t t l e e a s i e r t h a n
t h e p r e v i o u s o n e s s i n c e t h e b u l k o f t h e w o r k
w i l l b e d o n e b y t h e T I P d e v i c e s , a l o w c o s t
g e n e r a l p u r p o s e t r a n s i s t o r i s s u i t a b l e , l i k e
B C 1 0 8 , 2 N 2 2 2 2 s h o u l d b e f i n e .
A n i m p o r t a n t a s p e c t o f t h i s c i r c u i t i s t h a t t h e
c o m m o n " g r o u n d o f t h e R a s p b e r r y P i a n d t h e
m o t o r p o w e r s u p p l y . T h i s a l l o w s t h e t w o
s e p a r a t e p o w e r s u p p l i e s t o b e t i e d t o t h e
s a m e " 0 V " a l l o w i n g t h e m t o w o r k i n t h e s a m e
c i r c u i t ( a l t h o u g h w e s t i l l h a v e t o t a k e c a r e t o
k e e p t h e M o t o r S u p p l y + V a w a y f r o m t h e
G P I O ) .
R e s i s t o r V a l u e s
W h e n y o u s t a r t t o c a l c u l a t e t h e n u m b e r s , y o u
w i l l n o t i c e w e h a v e a l o t o f h e a d r o o m , s o w e
m a y a s w e l l a i m f o r t h e i d e a l o f o n l y u s i n g a
5 t h o f t h e g a i n , w h i c h m e a n s t h e t r a n s i s t o r i s
n o t o n l y s a t u r a t e d , b u t n o t b e i n g p u s h e d t o o
h a r d ( e v e n i f t h e m o t o r v o l t a g e i s r a i s e d o r
l o w e r e d , o r m o r e c u r r e n t d r i v e n u p t o 5 A ) . W e
w i l l u s e a 9 V m o t o r s u p p l y f o r t h e c a l c u l a t i o n s .
Q 1 , Q 2 a s s u m e g a i n o f 2 0 0 ( 1 / 5 o f h
F E ( m i n )
) ,
IB = IC/(hFE(min)/5)
= 2.5A/200=12.5mA
R 3 = ( Vmotor-Q1VCB-Q2VBE)/IB= (9 1.4 1.4) / 0.0125 = 496 ohm
( 4 7 0 o h m g i v e s 1 3 m A , g a i n 1 9 0 )
Q 5 a s s u m e g a i n o f 6 ( 1 / 5 o f H
F E ( m i n )
) ,
IB = 0.0125/6 = 2mA
R 1 = ( VGPIO-Q5VBE-Q2VBE)/IB= (3.3 0.7 - 1.4) / 0.002 = 600 ohm
( 6 8 0 o h m g i v e s 1 . 7 6 m A , g a i n 7 . 5 )
S o w e c a n u s e 6 8 0 o h m f o r R 1 & R 2 a n d
4 7 0 o h m f o r R 3 & R 4 .
C o n t r o l
W i t h t h e e x t r a c o n t r o l t r a n s i s t o r s , i t i s e a s y t o
c o n t r o l t h e m o t o r , t u r n o n Q 5 f o r f o r w a r d a n d
t u r n o n Q 6 t o r e v e r s e . I t s h o u l d b e o b v i o u s
t h a t y o u d o n o t w a n t t o t r y f o r w a r d a n d r e v e r s e
a t t h e s a m e t i m e !
Y o u c a n c h e c k y o u r p r o g r a m b y c o n n e c t i n g
t h e G P I O p i n s t o L E D s i n s t e a d o f t h e H - B r i d g e
t o c o n f i r m y o u w i l l o b t a i n t h e c o r r e c t o u t p u t s .
N o t e : P a g e s 6 / 7 o f t h i s a r t i c l e h a v e b e e n u p d a t e d t o r e p l a c e
t h e o r i g i n a l c i r c u i t . O t h e r i m p r o v e d m o t o r c o n t r o l s o l u t i o n s w i l l
b e e x p l o r e d i n l a t e r i s s u e s .
U p d a t e d b y M e l t w a t e r
A r t i c l e b y B o d g e N H a c k i t t
B a s i c G P I O H B r i d g e C o n t r o l . p y :
#! /usr/bin/python
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
Q5=7; Q6=11 # Set GPIO Pins
#----------------------------IMPORTANT ------------------------------
#IF Q5 ON THEN Q6 must be OFF
# ELSE transistor short circuit !
#--------------------------------------------------------------------
#Set Starting State
GPIO.setup(Q5, GPIO.OUT); GPIO.setup(Q6, GPIO.OUT)
GPIO.output(Q5, False); GPIO.output(Q6, False)
print "Drive motor forwards for 3 seconds"
GPIO.output(Q5, True)
time.sleep(3)
print "Stop motor"
GPIO.output(Q5, False)
print "Drive motor in reverse for 3 seconds"
GPIO.output(Q6, True)
time.sleep(3)
print "Stop motor"
GPIO.output(Q5, False); GPIO.output(Q6, False)
GPIO.cleanup()
N O T E : T h i s p r o g r a m w o u l d n e e d a n o t h e r s e t o f 2 G P I O p i n s t o c o n t r o l a s e c o n d H b r i d g e i f t h e
r o b o t i s u s i n g t w o m o t o r s .
7
7/28/2019 The MagPi 2012 12 Issue 8
8/34
T h i s a r t i c l e c o v e r s a n e a s y t o b u i l d , c h e a p
a n d a b o v e a l l , s a f e w a y t o c o n t r o l m a i n s
p o w e r e d d e v i c e s w i t h a R a s p b e r r y P i . N o t h i n g
i n t h i s p r o j e c t i n v o l v e s g o i n g a n y w h e r e n e a r
a n y d a n g e r o u s v o l t a g e s . S o l d e r i n g i s l i m i t e d
t o j u s t a f e w j o i n t s a n d s o u r c e c o d e c a n b e
d o w n l o a d e d f o r t h e s o f t w a r e p a r t .
T h e S t o r y
I b o u g h t s o m e i n e x p e n s i v e r e m o t e c o n t r o l l e d
p o w e r s o c k e t s f r o m h t t p : / / w w w . a m a z o n . c o . u k
( s e a r c h S t a t u s r e m o t e c o n t r o l s o c k e t ) . [ E d : I
s a w s o m e t h i n g v e r y s i m i l a r f o r $ 2 0 i n H o m e
D e p o t ] . I t r i e d t h e m o u t w i t h a f e w r a n d o m
a p p l i a n c e s , s t u c k t h e m i n t h e c u p b o a r d a n d
f o r g o t a b o u t t h e m . . . b u t t h e R a s p b e r r y P i
i n s p i r e d m e t o d o s o m e t h i n g u s e f u l w i t h t h e m .
I d e c i d e d t o u s e t h e R a s p b e r r y P i t o r e p l a c e
t h e r e m o t e c o n t r o l a n d b e c a u s e i t i s
p r o g r a m m a b l e , w e c a n e x t e n d t h e c a p a b i l i t y .
H o w a b o u t t u r n i n g C h r i s t m a s t r e e l i g h t s o n
a n d o f f a t s e t t i m e s , u s e y o u r s m a r t p h o n e t o
t u r n o n t h e k e t t l e j u s t a s y o u a r r i v e h o m e o r
f l a s h t h e b e d s i d e l i g h t w h e n S a n t a w a l k s o n a
p r e s s u r e s e n s i t i v e m a t i n f r o n t o f t h e c h i m n e y
o n C h r i s t m a s E v e !
D e c o d i n g t h e R e m o t e C o d e s
T h e f i r s t s t e p w a s t o i d e n t i f y t h e s i g n a l l i n g
u s e d b y t h e s o c k e t s s o I c o u l d t r y t o m i m i c i t
u s i n g t h e R a s p b e r r y P i . T h e c o m m u n i c a t i o n
b e t w e e n t h e r e m o t e a n d t h e s o c k e t s i s r a d i o
b a s e d , a l l o w i n g c o n t r o l w i t h o u t l i n e o f s i g h t .
T h i s a l l o w s s o c k e t s i n v a r i o u s r o o m s t o b e
c o n t r o l l e d f r o m o n e l o c a t i o n .
T h e r e s a s t i c k e r o n t h e b a c k o f t h e s o c k e t s
s a y i n g 4 3 3 . 9 2 M H z , s o I s e a r c h e d e B a y f o r
4 3 3 M H z r e c e i v e r t o f i n d a s u i t a b l e r e c e i v e r .
T h i s f o u n d m e a t r a n s m i t t e r a n d r e c e i v e r f o r
A r d u i n o p r o j e c t s f o r o n l y 1 . 6 0 ( $ 1 . 9 9 )
i n c l u d i n g p o s t a g e a n d p a c k i n g f r o m C h i n a !
I h a d t o s o l d e r a 7 " ( 1 7 c m ) a n t e n n a w i r e o n t o
t h e r e c e i v e r a n d t r a n s m i t t e r . T h e a n t e n n a
l e n g t h r e p r e s e n t s a 1 / 4 w a v e l e n g t h o f
4 3 3 . 9 2 M H z . Y o u c a n u s e a n o n l i n e c a l c u l a t o r
s u c h a s h t t p : / / w w w . c s g n e t w o r k . c o m /
f r e q w a v e l e n g t h c a l c . h t m l .
T o r e a d t h e c o d e f r o m t h e r e m o t e c o n t r o l , I
c o n n e c t e d t h e r e c e i v e r m o d u l e t o a c o m p u t e r
m i c r o p h o n e s o c k e t . I u s e d a s e p a r a t e 5 V
p o w e r s u p p l y , b u t t h e r e s a 5 V o u t p u t o n t h e
A u t o m a t e y o u r h o m e w i t h a R a s p b e r r y P i . . . a n d c a t c h S a n t a
i n t h e a c t !
8
7/28/2019 The MagPi 2012 12 Issue 8
9/34
R a s p b e r r y P i G P I O t h a t w o u l d s e r v e t h e
p u r p o s e . T h e o u t p u t f r o m t h e r e c e i v e r m o d u l e
i s 5 V d i g i t a l , w h i c h i s n t s u i t a b l e t o b e
c o n n e c t e d s t r a i g h t i n t o a c o m p u t e r a u d i o
s o c k e t , s o I c o n n e c t e d t h e o u t p u t o f t h e
m o d u l e v i a a 1 M O h m r e s i s t o r t o t h e
m i c r o p h o n e s o c k e t o f m y l a p t o p . I a s s e m b l e d
t h e c i r c u i t o n b r e a d b o a r d , b u t y o u c o u l d u s e
t h e o t h e r e n d o f t h e f l o p p y d i s k d r i v e c a b l e
c o n n e c t o r I m e n t i o n i n t h e H a r d w a r e s e c t i o n .
A u d a c i t y ( h t t p : / / a u d a c i t y . s o u r c e f o r g e . c o m ) i s
e x c e l l e n t f r e e w a r e f o r e x a m i n i n g s i g n a l s l i k e
t h i s . O n c e I w a s s a t i s f i e d t h a t t h e r e c o r d i n g
l e v e l w a s a b o u t r i g h t , I s t a r t e d r e c o r d i n g a n d
p r e s s e d o n e o f t h e b u t t o n s o n t h e r e m o t e .
H a v i n g s t o p p e d t h e r e c o r d i n g I w a s a b l e t o
z o o m i n o n t h e a r e a w h e r e t h e s i g n a l f r o m t h e
r e m o t e w a s . T h e s i g n a l r e p e a t e d o v e r a n d
o v e r u n t i l t h e r e m o t e b u t t o n w a s r e l e a s e d . T h e
n a r r o w p u l s e s s e e m e d t o b e a b o u t 0 . 2 5 m s i n
d u r a t i o n w i t h t h e w i d e p u l s e s 3 t i m e s a s l o n g .
F r o m t h e w a v e f o r m I c r e a t e d t h i s b i n a r y s t r i n g .
E a c h b i t r e p r e s e n t s 0 . 2 5 m s w i t h 1 = h i g h
p u l s e a n d 0 = l o w p u l s e . T o a i d r e a d a b i l i t y
e a c h p u l s e h a s b e e n s e p a r a t e d w i t h a d a s h .
11111-000-1-000-1-000-1-000-1-000-1-000-
1-000-1-000-1-0-111-000-1-0-111-000-1-
000-1-0-111-000-1-000-1-0-111-0-111-0-
111-0-111-0-111-000-1-000-1-000-1-0-
111111
T h i s i s m a k e o r b r e a k t i m e f o r t h e p r o j e c t . I f
y o u h a v e c o m e t h i s f a r a n d c a n n o t f i n d a
r e p e a t i n g p a t t e r n , t h a t a p p e a r s w h e n y o u
p r e s s t h e r e m o t e c o n t r o l b u t t o n , y o u r s o c k e t s
m a y n o t b e u s i n g t h e s i m p l e A M s i g n a l l i n g t h a t
t h i s p r o j e c t r e l i e s o n .
S e n d i n g a S i g n a l
T o b e a b l e t o d o a n y t h i n g w i t h t h e c a p t u r e d
d a t a , I c o n n e c t e d t h e t r a n s m i t t e r t o t h e
R a s p b e r r y P i . T h e t r a n s m i t t e r i s i n t e n d e d t o
s t a y c o n n e c t e d t o t h e R a s p b e r r y P i , s o i t s
p o w e r e d f r o m t h e + 5 V p i n o f t h e G P I O . I
c o n n e c t e d G P I O 7 o f t h e R a s p b e r r y P i t o t h e
d a t a p i n a n d t h e G N D o f t h e t r a n s m i t t e r t o t h e
G N D o f t h e R a s p b e r r y G P I O c o n n e c t o r . T h e
3 . 3 V s i g n a l s a r e e n o u g h t o d r i v e t h e
t r a n s m i t t e r , t h o u g h I o n l y f o u n d t h i s o u t b y
t r y i n g i t .
B e c a u s e L i n u x i s a m u l t i t a s k i n g O S a n d
s o m e t h i n g e l s e c o u l d u s e t h e C P U a t j u s t t h e
w r o n g t i m e , m y p r o g r a m r e p e a t e d l y s e n d s t h e
b i t s e q u e n c e 1 0 t i m e s w i t h t h e e x p e c t a t i o n
t h a t a t l e a s t o n e w i l l b e t r a n s m i t t e d c o r r e c t l y .
W h e n I r a n m y s o f t w a r e o n t h e R a s p b e r r y P i
f o r t h e f i r s t t i m e , I c a p t u r e d t h e s i g n a l w i t h
A u d a c i t y . I c o u l d s e e t h a t t h e w a v e f o r m s h a p e
w a s c o r r e c t , b u t u p s i d e d o w n . N e e d l e s s t o
s a y , t h e s o c k e t d i d n t d o a n y t h i n g . I f l i p p e d a l l
t h e b i t s i n m y o u t p u t s t r e a m , a n d r e - r a n t h e
t e s t c o d e . T h i s t i m e t h e s o c k e t s w i t c h e d o n !
T h i s j u s t l e f t m e w i t h t h e o t h e r b u t t o n s t o
t r a n s c r i b e .
H a r d w a r e
T h e c o n n e c t o r f o r t h e G P I O a n d t h e c a b l e I
u s e d i s f r o m a n o l d P C f l o p p y d i s k d r i v e c a b l e .
T h e s e h a v e a w i d e r c a b l e a n d p l u g t h a n t h e
R a s p b e r r y P i G P I O c o n n e c t o r , b u t y o u c a n f i t
t h e p l u g o n t o t h e G P I O p i n s w i t h p a r t o f t h e
p l u g g o i n g o f f t h e e n d . O f c o u r s e , t h i s w o n t
w o r k i f y o u r R a s p b e r r y P i i s i n a c a s e .
O n l y 3 w i r e s a r e r e q u i r e d f r o m t h e G P I O ; + 5 V ,
G N D a n d G P I O 7 ( C E 1 ) , t h e p i n I u s e t o
c o n t r o l t h e t r a n s m i t t e r .
O n t h e o t h e r e n d o f m y r i b b o n c a b l e i s a v e r y
s m a l l p i e c e o f s t r i p b o a r d . S o l d e r e d t o t h i s
b o a r d i s a c o n n e c t o r f o r t h e t r a n s m i t t e r , m a d e
b y c u t t i n g d o w n a n I C s o c k e t .
C o n t i n u e d o v e r p a g e . . .
9
7/28/2019 The MagPi 2012 12 Issue 8
10/34
S o f t w a r e
A s t h e G P I O c o d e i n t h e e x a m p l e I f o l l o w e d
f r o m h t t p : / / w w w . e l i n u x . o r g / R p i _ L o w - l e v e l _
p e r i p h e r a l s d i r e c t l y a c c e s s e d m e m o r y i t
n e e d e d t o b e r u n b y t h e r o o t u s e r . T o m a k e
m y l i f e e a s y I d e v e l o p e d a n d t e s t e d i t w h i l e
l o g g e d i n a s r o o t . A l l c o m m a n d s g i v e n h e r e
a s s u m e y o u w i l l b e d o i n g t h e s a m e .
E v e r y t h i n g i s d o n e f r o m t h e c o m m a n d l i n e s o ,
u n l e s s y o u r s y s t e m i s s e t t o j u m p s t r a i g h t t o
t h e g r a p h i c a l f r o n t e n d , j u s t s t a y a t t h e
c o m m a n d l i n e .
T o c r e a t e a d i r e c t o r y i n w h i c h t o k e e p t h e
s o u r c e c o d e a n d t h e e x e c u t a b l e f i l e w h i l e
u n d e r d e v e l o p m e n t , t y p e t h e f o l l o w i n g :
$ mkdir gpio
$ cd gpio
M y s o u r c e c o d e c a n b e d o w n l o a d e d f r o m
h t t p : / / w w w . h o a g i e s h o u s e . c o m . J u s t f o l l o w t h e
l i n k s a n d s a v e t h e f i l e i n t h e d i r e c t o r y y o u j u s t
c r e a t e d . I t t a k e s 2 p a r a m e t e r s , t h e c h a n n e l
a n d o n o r o f f . Y o u l l n e e d t o e d i t t h e c o d e t o
r e p l a c e m y r e m o t e c o n t r o l c o d e s w i t h y o u r
o w n . T o e d i t t h e c o d e t y p e :
$ nano switch.cpp
B e c a r e f u l l t o r e t a i n t h e q u o t e m a r k s a r o u n d
t h e c o d e s i n t h e s o u r c e . W h e n y o u h a v e
c h a n g e d t h e c o d e s , q u i t b y p r e s s i n g -
, a n s w e r
Yt o t h e q u e s t i o n a b o u t s a v i n g t h e
f i l e a n d a c c e p t t h e f i l e n a m e s w i t c h . c p p . T o
b u i l d t h e e x e c u t a b l e f i l e , t y p e :
$ g++ -o switch switch.cpp
T e s t i t w i t h y o u r s o c k e t s b y t y p i n g :
$ ./switch 1 on
I f i t w o r k s , y o u w i l l p r o b a b l y w a n t t o b e a b l e t o
r u n i t a s a n y u s e r . T y p e t h e f o l l o w i n g
c o m m a n d s t o d o t h i s :
$ chmod +s switch
$ mv switch /usr/bin/
T o s c h e d u l e t h e s o c k e t s t o c o m e o n a n d o f f a t
c e r t a i n t i m e s , y o u c a n u s e s o m e t h i n g c a l l e d
c r o n j o b s . J u s t t y p e :
$ crontab e
Y o u w i l l b e a b l e t o e d i t a f i l e t h a t c o n t r o l s
s c h e d u l e d j o b s . T h e f o r m a t i s d e s c r i b e d i n t h e
f i l e , b u t t o t r y s o m e t h i n g o u t , a d d t h e s e l i n e s t o
t h e b o t t o m o f t h e f i l e :
0 * * * * switch 1 on
10 * * * * s witch 1 off
T h i s w i l l t u r n s o c k e t o n e o n f o r t h e f i r s t t e n
m i n u t e s o f e v e r y h o u r .
S o f a r , t h i s i s n o t v e r y u s e r f r i e n d l y . A w e b
b a s e d i n t e r f a c e w o u l d b e a l o t n i c e r . M y w e b
i n t e r f a c e a l l o w s t h e 4 c h a n n e l s t o b e s w i t c h e d
o n a n d o f f , b u t I v e n o t g o n e a s f a r a s t o a d d
a n y s c h e d u l i n g t o i t . F i r s t , i n s t a l l m i n i - h t t p d t o
a c t a s t h e w e b s e r v e r . T o d o t h i s t y p e :
$ apt-get install mini-httpd
Y o u c a n d o w n l o a d t h e f i l e s f o r t h e w e b
i n t e r f a c e f r o m h t t p : / / w w w . h o a g i e s h o u s e . c o m .
I n t h e z i p f i l e i s t h e c o n f i g f i l e f o r m i n i - h t t p d
a n d t h e v a r / w w w d i r e c t o r y , w h i c h i s w h e r e I
p u t w e b p a g e a n d c g i p r o g r a m s . T h e s e
s h o u l d n t n e e d a n y m o d i f i c a t i o n , b u t w i l l n e e d
t o b e c o p i e d t o t h e c o r r e c t l o c a t i o n s . T h e
H T M L f i l e u s e s a p r i m i t i v e A J A X r e q u e s t t o
r u n t h e C G I s c r i p t w i t h t h e c h a n n e l a n d o n / o f f
p a r a m e t e r s . T h e C G I s c r i p t j u s t p u l l s t h e
p a r a m e t e r s f r o m t h e q u e r y a n d c a l l s t h e
s w i t c h p r o g r a m w i t h t h e m .
C o n c l u s i o n
N o w t h a t y o u h a v e t h e s o f t w a r e f o u n d a t i o n t o
r e m o t e l y c o n t r o l m u l t i p l e m a i n s d e v i c e s w i t h
y o u r R a s p b e r r y P i , I l e a v e i t a s a n e x e r c i s e f o r
y o u t o i m a g i n e w h a t o t h e r t r i g g e r s y o u c o u l d
c o n n e c t t o t h e G P I O t o t u r n o n a n d o f f
d e v i c e s . O h , i f y o u d o c a t c h S a n t a p l e a s e t e l l
h i m I w o u l d l i k e t h e p r i n t e d M a g P i C h r i s t m a s
P a c k f r o m h t t p : / / w w w . k i c k s t a r t e r . c o m /
p r o j e c t s / t h e m a g p i / t h e - m a g p i - m a g a z i n e - f r o m -
v i r t u a l - t o - r e a l i t y .
M a p l i n P a r t s L i s t
V Y 4 8 C - 4 3 3 M H z T X / R X 9 . 9 9
M 1 M - 1 M O h m r e s i s t o r 0 . 2 9
D G 4 1 U - F l o p p y d i s k c a b l e 3 . 9 9
A r t i c l e b y G e o f f J o h n s o n
0
7/28/2019 The MagPi 2012 12 Issue 8
11/34
To see the large range of PCSL brand Raspberry Pi accessories visit
h t t p : / / w w w . p c s l s h o p . c o m
Last Month's Winners!The 5 winners of the PCSL Raspberry Colour Case are
D a v e H e n e g h a n ( C h o r l e y , U K ) ,
D e a n H u t c h i s o n ( G l a s g o w , U K ) , D a v e C a r n e y ( H a r t l e p o o l , U K ) , N i g e l L a u d a t
( L i v e r p o o l , U K ) and
P e t e r L o c a s t r o ( D e r b y , U K ) .
Congratulations. PCSL will be emailing you soon with details of how to claim all of those
fantastic goodies!
This month's prize is a new 512MbRaspberry Pi Model B plus a 1A 5V power
supply and a PCSL Raspberry Pi case!
Both the 2nd and 3rd prize winners will
each receive a PCSL Raspberry Pi case.
For a chance to take part in this month's
competition visit:
h t t p : / / w w w . p c s l s h o p . c o m / i n f o / m a g p i
Closing date is 20th December 2012.
Winners will be notified in the next issue of
the magazine and by email. Good luck!
The MagPi and PC Supplies Limited are very proud to announce a veryspecial prize for the winner of this month's competition.
DECEMBER COMPETITION
11
7/28/2019 The MagPi 2012 12 Issue 8
12/34
I n t r o d u c t i o n
An Arduino board can communicate with theRaspberry Pi via a serial over USBconnection. This creates a virtual serialinterface, which it uses like a normal interface,reading and writing to the serial device file. Tobegin, attach your Arduino board and type:
$dmesg | tail
[..]usb 1-1.2: Manufacturer: Arduino[..]
[..]cdc_acm 1-1.2:1.0: ttyACM0: USB ACM
device[..]
My Arduino Uno board device is /dev/ttyACM0and its driver is cdc_acm. Old arduino boardswith a FTDI USB-Serial chip are accessed via
/dev/ttyUSB*:
$ls -l /dev/ttyACM*
crw-rw---T 1 root dialout 166, 0 Nov 5
00:09 /dev/ttyACM0
Ok, now you should add your user to the'dialout' group to give the required read/writeaccess, then logout and login again for this totake effect:
$sudo usermod -a -G dialout YOURUSERNAME
This is important because Nanpy works usingthis device file. Nanpy is an open sourceproject relased under the MIT license, and iscomposed of a server part (flashed to yourArduino which waits for commands on aserial) and a pure Python library. This libraryallows you to communicate with your Arduinoconnected via USB using classes andmethods really similar to the Arduinoframework's ones. Behind the scenes whenyou create/delete an object or call methodswith Python, Nanpy communicates via USBand asks the server part to create/delete thecorresponding object and calls methods in
Arduino for you: you can instantiate how manyobjects you want without worrying aboutdeallocation and it's also possible to use in amultithreading context. Nanpy aims to makedevelopers' lives easier, giving them a simple,clear and fast instrument to create prototypesand scripts interacting with Arduino, saving alot of time. To install Nanpy read the
README file. You need to install Arduino onyour laptop or your Raspberry Pi in order tobuild the firmware:
$sudo apt-get install arduino
Nanpy is actually under heavy developmentand it's only been tested on the Uno board.You can get Nanpy from the Pypi page(http://pypi.python.org/pypi/nanpy) or Github
(https://github.com/nanpy).Let's see Nanpy in action and try to turn on aLED placed in the 13th pin of the Arduino:
from nanpy import Arduino
Arduino.pinMode(13, Arduino.OUTPUT)
Arduino.digitalWrite(13, Arduino.HIGH)
Arduino provides all the main functions, delay,analog/digital write and read. No setup or loopfunctions, just objects and method calls. In
fact, Nanpy supports all of the main Arduinomethods - LCD, Tone, Stepper and otherlibraries. Now let's see how to use our 16x2text-based LCD on pins 6, 7, 8, 9, 10, 11,writing a better Hello World script:
from nanpy import Lcd
lcd = Lcd([7, 8, 9, 10, 11, 12],[16, 2])
lcd.printString("Hello World!")
J u s t a w o r d o f w a r n i n g : Raspberry Pi may
not provide enough power to drive yourArduino, so you might need to connectArduino to an external power source.
C o n t r o l y o u r A r d u i n o b o a r d
w i t h R a s p b e r r y P i a n d P y t h o n
The power of Raspberry and the simplicity of Arduino
using Python and a simple library: Nanpy.
2
7/28/2019 The MagPi 2012 12 Issue 8
13/34
T h e e x t e r n a l w o r l d
Now I want to show you how to make Arduinocommunicate with the external world using theRaspberry Pi. To understand it we will build amodern clock, able to measure externaltemperature, with an alarm initialised viabluetooth (using an Android device in this
case) and date and time updated via a ntpserver...
You can find the project with instructions, anAndroid app and required components here:https://github.com/nanpy/eggsamples/tree/master/synclock. To show how Nanpy works in amultithreading context, this program creates athread for every functionality, writing it all on
the same LCD. In this article I show only theinner part of every "while True" cycle presentin each "run" method, so I recommend youfollow along with the source code. Let's startwith the main thread, TimeThread, that readsthe time from our ntp server every one secondand stores it in a global variable, milltime:
response = ntplib.NTPClient().request('europe.pool.ntp.org',version=3)
milltime = int(response.tx_time)
To show date and time on the LCD, create asecond thread, ShowTimeThread:...self.servo = Servo(12)...dt = datetime.fromtimestamp(milltime)lcd.printString(dt.strftime('%Y/%m/%d'),
0, 0)lcd.printString(dt.strftime('%H:%M'),
0, 1)
self.servo.write(90 + (30 * self.c))self.c *= -1
Every second we get the milltime global
variable, trasform it to a readable format andthen print the date and time onto the LCD. Asyou can see, printString can be calledspecifying the position (column, row) you wishthe string to appear on the LCD. Then wemove the servo motor like a pendulum everysecond. We can update the temperature inanother thread. Reading the value of our
temperature sensor from the analog pin 0 andprinting it on the LCD, near the time, every 60seconds:
temp = ((Arduino.analogRead(0) / 1024.0)* 5.0 - 0.5) * 100
lcd.printString("- %0.1f\xDFC" % temp,6, 1)
Ok, now let's see how to communicate with anAndroid phone that can set the alarm clock viabluetooth. I paired my device with theRaspberry Pi before start, follow this guide todo that: http://wiki.debian.org/BluetoothUser.Remember to install python-bluez too. We willuse AlarmClock, a thread-safe class, to saveon disk and get from it the alarm clock value(look at the code). Then we can start ourbluetooth communication in another thread,AlarmClockThread:
...Bluetooth init and connection...
cli_sock, cli_info = srv_sock.accept()cli_sock.send("%d:%d:%d", ck.getAlarm())try:
while True:data = cli_sock.recv(3)if len(data) == 0: breakck.setAlarm(ord(data[0]),
ord(data[1]),ord(data[2]))
except IOError:pass
Our Raspberry Pi acts as a server, waiting fora bluetooth connection: once this happens, itsends the alarm clock to our device and waitsfor a new value to store. In the TimeThread wecompare the actual time with the alarm value:if they match we can start another thread,PlayAlarmThread, playing a C note for 250ms,five times, using a Tone object through aspeaker controlled via the 4th digital pin. It'stime to wake up!
Start thinking about your own project withNanpy, for example trying to bring your old RCcar back to life: youtu.be/NI4PDfVMdgM
Article by Andrea Stagi
13
7/28/2019 The MagPi 2012 12 Issue 8
14/34
S u m m a r y
Controlling hardware that is hooked up to thePi is really fun. Controlling hardware fromacross town or another state that is hooked up
to the Pi is awesome.
We are controlling five servos; each servocontrols a needle on a chart that can show anydata we choose through printable, modularbackgrounds. We used PHP to create awebpage that is served up by the Pi. The PHPmakes system calls through the command linethat calls a Python script. In turn, the Pythonscript controls the movement of the servos
over an I2
C bus; it also returns the positions ofthe servos by reading values out of a registerthat lives on the servo driver. The 16-channelservo driver is from Adafruit(http://www.adafruit.com/products/815 ); itcomes with a nice library that takes care oflow level operations. You need their tutorial forinitial set up and library downloads. We haveprovided all our code and templates along witha help file in a Git repository. This project canbe scaled to control up to 16 servos.
We used the newest Debian Wheezydistribution to develop the code on a Type BRev1 Raspberry Pi. A Rev2 board can beused with some modifications to the library.
B i l l o f M a t e r i a l s
Here is a list of parts you will need to completethis project:
Adafruit servo driver datasheet:http://www.adafruit.com/datasheets/PCA9685.pdf
H o o k i n g U p H a r d w a r e
For safety, shutdown your Pi and removepower before making any connections.
$ sudo shutdown -h now
First, connect to the servos. Most servos comewith mating connectors pre- installed. Plug theconnector into the servo driver, but make surethe colors match the silkscreen. We used Ch.
1-5
Black = GroundRed = V+Yellow = Signal
The Pi cannot source enough current to powerthe servos. Thus, you need an external powersupply. We used a wall wart (AC adapter)from an old +5VDC cell phone charger that we
had on hand. Use the terminal block on theservo driver to make the V+ and GNDconnections.
P i G a u g e
This fun project shows how to control servo motors over
the internet using a RaspberryPi.
DIFFICULITY: Easy-Medium
14
7/28/2019 The MagPi 2012 12 Issue 8
15/34
Lastly, connect the Pi to the servo controller.This requires four connections from the GPIOheader on the Pi to the header on the servodriver: 3.3V, GND, SDAand SCL.
Double check ALL yourconnections BEFOREapplying power.
C a u t i o n : The Vcc and V+ pins are adjacent to
each other on the servo driver, don't mix themup like we did or you will have a stale Pi!
Plug in your wall wartand power up your Pi.
If you connectedeverything correctly
you will not see orsmell any magicsmoke.
D o w n l o a d S o f t w a r e a n d T o o l s
Although not mandatory, it is a good idea tokeep your Pi up to date; start with:
$ sudo apt-get update && sudo apt-get upgrade
Save the files in your home directory:
$ sudo apt-get install git$ git clone https://github.com/Thebanjodude/PiGauge
Comment out all lines in this file:
$ sudo nano /etc/modprobe.d/raspi-blacklist.conf
Add the I2C device to the kernal. Restart yourPi then add yourself to the I2C group:
$ sudo modprobe i2c-dev$ sudo usermod -aG i2c yourusername
I n s t a l l A p a c h e a n d P H P
$ sudo apt-get install apache2 php5 libapache2-mod-php5
To find the IP of your Pi (i.e. 192.168.1.10):$ ip addrinet: ip.of.your.pi
Go to http://ip.of.your.pi and you should seethe "It Works!" page.
Link the PiGauge Project to www root:
$ cd /var/www$ sudo ln -s /home/pi/PiGauge
Add apache to the I2C group to allow it toaccess the I2C bus. Then restart apache:
$ sudo adduser www-data i2c$ sudo /etc/init.d/apache2 restart
From your home directory:
$ sudo cp ./Adafruit-Raspberry-Pi-Python-Code/Adafruit_PWM_Servo_Driver/Adafruit_I2C.py
/usr/local/lib/python2.7/site-packages/
$ sudo cp ./Adafruit-Raspberry-Pi-Python-Code/Adafruit_PWM_Servo_Driver/Adafruit_PWM_Servo_Driver.py /usr/local/lib/python2.7/site-packages/
You should be ready to go, head over tohttp://ip.of.your.pi/PiGauge/and try it out!
R e a d i n g S e r v o P o s i t i o n s
In this code snippet we are adding twounsigned bytes from the I2C bus to get theposition of a servo.
S p o n s o r e d b y N a t i o n a l T e c h n i c a l S y s t e m s
A l b u q u e r q u e E n g i n e e r i n g S e r v i c e s
http://www.nts.com/locations/albuquerque
Continued over page...
15
7/28/2019 The MagPi 2012 12 Issue 8
16/34
S p o n s o r e d b y N a t i o n a l T e c h n i c a l S y s t e m s
A l b u q u e r q u e E n g i n e e r i n g S e r v i c e s
http://www.nts.com/locations/albuquerque
def print_position(chart_num):chart_pos = (chip.readU8(8 + chart_num * 4)
+(chip.readU8(9 + chart_num * 4) >8). Then you'llbe able to add them together for a 16-bit
number. The cool thing about registers is thatthe electronics don't care what is in them. It iscompletely up to you, the programmer, tointerpret what is inside them.
M o v i n g S e r v o s
The whole project revolves around movingservo motors. The following lines of code arearguably the most critical. We defined afunction called move_servos(). This function
takes two arguments: which chart number youwant to move and where you want to move it.pwm.setPWM() comes from the Adafruitlibrary.
def move_servos(chart_num, chart_pos):pwm.setPWM(chart_num,0,chart_pos)time.sleep(0.1)
chart_pos is a number between 170 and 608,but will vary a little from servo to servo. Thesenumbers relate to a pulse width time (look upservo control if you are interested). To make
the software more intuitive we have scaled thenumbers from 0-100 using a transfer function,then we took it one step further. Since servos
are not exactly linear, we took some datapoints, named servo_data, and coded a linearregression (a fancy word for line of best fit) tomake up for the non-linearities of the servos.The linear regression function returns thevariables xfer_m and xfer_b that are usedbelow.
def transfer(chart_percent):return int(xfer_m * chart_percent + xfer_b)
def inverse_transfer(chart_pos):return int(round((chart_pos - xfer_b) / xfer_m))
S o f t w a r e T e s t i n g
We are big believers in the Agile softwaredevelopment methodology and incremental
progress. We didn't deploy the use of scrumsor tracking in this little project but we didmake some lightweight unit tests; they are
available in the repository if you find yourselfcurious.
A c k n o w l e d g e m e n t s
Special thanks to Scott Ehlers for patientlyteaching me some new UNIX and PHP skills
and to Tanda Headrick for building themechanical display. A very special thanks toNational Technical Systems (NTS) forsponsoring the project by giving us a bit ofplaytime to build a project status display.Follow the NTS links for more information onwhat we do when we arent playing with aRaspberry Pi.
Article by Ben Schaefer
16
http://www.nts.com/locations/albuquerque7/28/2019 The MagPi 2012 12 Issue 8
17/34
S a f e a n d s i m p l e c o n n e c t i o n t o y o u r R a s p b e r r y P i
H a r d w a r e
I n t e r f a c e b o a r d
I
2
C P o r t E x t e n d e r
A n a l o g u e b o a r d
S o f t w a r e
F o r G P I O , I
2
C , S P I
D e v i c e L i b r a r i e s
E x a m p l e s
S u p p o r t
A r t i c l e s
T u t o r i a l s
F o r u m
F i n d o u t m o r e a t
q u i c k 2 w i r e . c o m
A 1
http://quick2wire.com/7/28/2019 The MagPi 2012 12 Issue 8
18/34
T h e M a g P i M a g a z i n e - F r o m V i r t u a l t o R e a l i t y h t t p : / / k c k . s t / T v k d v G
B r i n g T h e M a g P i m a g a z i n e , t h e b e s t a n d o n l y m a g a z i n e f o r t h e R a s p b e r r y P i e n t h u s i a s t ,
f r o m t h e d i g i t a l r e a l m i n t o t h e p h y s i c a l r e a l m .
T h i s i s a u n i q u e o p p o r t u n i t y t o
k i c k - s t a r t m o v i n g t h e a v a i l a b i l i t y o f
T h e M a g P i m a g a z i n e f r o m t h e
d i g i t a l r e a l m i n t o t h e p h y s i c a l
r e a l m a n d w e a r e o f f e r i n g
s u p p o r t e r s s o m e v e r y e x c i t i n g
r e w a r d s . T h e M a g P i i s t h e w o r l d ' s
o n l y m a g a z i n e f o r u s e r s o f t h e $ 3 5
R a s p b e r r y P i c o m p u t e r .
T h e M a g P i m a g a z i n e h a s b e e n
p u b l i s h e d o n l i n e e a c h m o n t h s i n c e
M a y 2 0 1 2 . E a c h 3 2 p a g e
m a g a z i n e i s c r e a t e d e n t i r e l y b y
e n t h u s i a s t s u s i n g t h e R a s p b e r r y
P i . O u r s i t e , w w w . T h e M a g P i . c o m ,
r e c e i v e s 1 0 0 , 0 0 0 h i t s e v e r y
m o n t h .
W e h a v e a l w a y s w a n t e d t o m a k e
t h e m a g a z i n e a v a i l a b l e i n p r i n t e d
f o r m , s o w e d i d a t e s t p r i n t r u n o f
i s s u e 6 . T h e r e s u l t w a s a l a r g e
n u m b e r o f r e q u e s t s f o r a l l b a c k
i s s u e s t o b e p r i n t e d . H o w e v e r
t h e r e a r e s i g n i f i c a n t u p f r o n t c o s t s
a s s o c i a t e d w i t h p r o f e s s i o n a l l y
p r i n t i n g a m a g a z i n e a n d a
s i g n i f i c a n t v o l u m e o f m a g a z i n e s
m u s t b e p r i n t e d t o g e t t h e p r i c e
p o i n t t o w h e r e w e w a n t i t t o b e .
Y o u r p l e d g e s w i l l b r i n g T h e M a g P i
f r o m v i r t u a l t o r e a l i t y . I f t h e r e i s
s u f f i c i e n t i n t e r e s t t h e n t h i s w i l l g i v e
u s c o n f i d e n c e t o c o n t i n u e t o m a k e
p r i n t e d c o p i e s o f T h e M a g P i
a v a i l a b l e w h e n w e s t a r t V o l u m e 2
n e x t y e a r .
R a t h e r t h a n m a k e i n d i v i d u a l b a c k
i s s u e s a v a i l a b l e , w e d e c i d e d i t
w o u l d b e b e t t e r a n d m o r e c o s t
e f f e c t i v e t o m a k e V o l u m e 1 o f T h e
M a g P i a v a i l a b l e a s o n e " b u m p e r
p a c k " . V o l u m e 1 w i l l c o m p r i s e o f
a l l 8 i s s u e s p u b l i s h e d i n 2 0 1 2 ,
i n c l u d i n g t h e D e c e m b e r i s s u e .
A d d i t i o n a l l y y o u g e t a n e x c l u s i v e
b i n d e r t o h o l d a l l t h e i n d i v i d u a l
m a g a z i n e s w i t h a r t w o r k w h i c h w i l l
b e d e s i g n e d e x c l u s i v e l y f o r t h i s
K i c k s t a r t e r p r o j e c t .
W i t h 3 2 p a g e s p e r m a g a z i n e , t h a t
r e p r e s e n t s 2 5 6 f u l l c o l o u r p a g e s o f
p u r e c o n t e n t .
T h e f u n d i n g g o a l i s n o t t h e r e a l
t a r g e t . W e n e e d a m i n i m u m o f
2 5 0 p l e d g e s f o r t h e m a g a z i n e
b e f o r e w e c a n c r e a t e a s u i t a b l e
p r i n t i n g r u n t o m a k e i t
w o r t h w h i l e f o r u s , a n d a g r e a t
p r i c e f o r y o u .
W h e r e w i l l t h e m o n e y g o ? H e r e a t
T h e M a g P i w e a r e a l l v o l u n t e e r s -
w e a r e c e r t a i n l y n o t i n t h i s f o r t h e
m o n e y . W e t a k e i n s p i r a t i o n f r o m
t h e R a s p b e r r y P i F o u n d a t i o n a n d
u s e o u r t a l e n t s , e n t h u s i a s m p l u s
a n e x t e n s i v e a m o u n t o f p e r s o n a l
t i m e t o g i v e b a c k t o t h e g l o b a l
c o m m u n i t y a n d h e l p f u r t h e r t h e
e d u c a t i o n o f a n y o n e u s i n g t h e
R a s p b e r r y P i c o m p u t e r .
H o w e v e r , t h e r e a r e c e r t a i n
a d m i n i s t r a t i v e c o s t s t h a t g o
t o w a r d s p r o d u c i n g a h i g h q u a l i t y
m a g a z i n e . P r i n t i n g m a g a z i n e s i s
c e r t a i n l y n o t f r e e p l u s t h e r e a r e
c o s t s r e l a t e d t o w e b s i t e h o s t i n g
a n d b a n d w i d t h u s a g e . W h i l e t h e
v a s t m a j o r i t y o f t h e p l e d g e a m o u n t
w i l l b e u s e d t o p r i n t t h e m a g a z i n e s
a n d p r o v i d e t h e r e w a r d s , t h e r e w i l l
b e a s m a l l s u r p l u s f r o m e a c h
p l e d g e . T h i s w i l l b e u s e d t o f u n d
t h e o n g o i n g c o s t s o f p r o d u c i n g
T h e M a g P i p l u s i t w i l l a l l o w u s t o
e x p l o r e o t h e r w a y s o f e x p a n d i n g
t h e a v a i l a b i l i t y o f t h e m a g a z i n e
a n d i n t r o d u c e o t h e r t y p e s o f
c o n t e n t .
W i t h e v e r y p l e d g e w e a l s o
m a k e a d o n a t i o n t o t h e
R a s p b e r r y P i F o u n d a t i o n .
M A G P I B U M P E R P A C K : T h i s i s
i t . Y o u g e t p r i n t e d c o p i e s o f
V o l u m e 1 ( 2 0 1 2 ) o f T h e M a g P i
c o m p r i s i n g o f a l l 8 i s s u e s . Y o u a l s o
g e t a l i m i t e d e d i t i o n M a g P i b i n d e r
t o s t o r e y o u r m a g a z i n e s p l u s
M a g P i s t i c k e r s . 2 5
G A M B L E P A C K : Y o u g e t t h e
M A G P I B U M P E R P A C K p l u s a
" G a m b l e " R a s p b e r r y P i c a s e . T h e
f u n g a m b l e i s t h a t y o u w i l l n o t
k n o w w h i c h c a s e c o l o u r y o u w i l l
r e c e i v e . 3 0
B O G O P A C K : B u y a M A G P I
B U M P E R P A C K a n d g i f t o n e . 5 0
M A G + P I K I T : G e t t h e M A G P I
B U M P E R P A C K a n d a 5 1 2 M b
R a s p b e r r y P i p l u s c a s e . 6 5
S T A R T U P K I T : T h i s g i v e s y o u t h e
M A G + P I K I T p l u s e v e r y t h i n g y o u
n e e d t o g e t y o u r R a s p b e r r y P i u p
a n d r u n n i n g . 9 9
S I G N A T U R E E D I T I O N : T h i s v e r y
l i m i t e d p l e d g e g i v e s y o u t h e
M A G P I B U M P E R P A C K p l u s a
s i g n e d c o p y o f t h e " R a s p b e r r y P i
U s e r G u i d e " b o o k w i t h a p e r s o n a l
m e s s a g e f r o m E b e n a n d L i z
U p t o n . 1 0 0
U L T I M A T E K I T : T h i s i s t o p - o f -
t h e - l i n e . Y o u g e t t h e S T A R T U P
K I T p l u s M O R E a c c e s s o r i e s . 1 4 5
S I G N A T U R E K I T : T h i s i s T H E
R a s p b e r r y P i e x p e r i e n c e . T h i s v e r y
l i m i t e d p l e d g e w i l l g i v e y o u t h e
U L T I M A T E K I T p l u s t h e
S I G N A T U R E E D I T I O N . 2 0 0
2
http://kck.st/TvkdvG7/28/2019 The MagPi 2012 12 Issue 8
19/34
PPrrooggrraammmmiinngg tthhee RRaassppbbeerrrryy PPii::GGeettttiinnggSSttaarrtteedd wwiitthh PPyytthhoonn
In a new book fromwww.raspberrypibook.com,
Simon Monk covers basic Python to in depth GPIO usage
Having bought a Raspberry Pi, chances arethat you will be interested in learning how toprogram your new gadget. The book"Programming the Raspberry Pi: GettingStarted with Python" b y S i m o n M o n k ,guides the reader through the process oflearning Python with the Raspberry Pi.
The book is accessible to newcomers toprogramming and leads the reader throughthe basics of Python, before moving on tomore complex topics such as using the Tkinterand Pygame libraries as well as programmingfor the GPIO connector.
The approach is very much hands-on.Programming concepts are developed inexample programs, which build from a simplestart in the same way as you would whenwriting a program from scratch.
Three chapters of the book are devotedexclusively to programming and using theGPIO connector. Various techniques, toolsand prototyping products are surveyed andexplained, including Gertboard, PiFace, PiCobbler and the RaspiRobotBoard.
Two of the hardware chapters are step-by-step instructions for building and programminghardware projects using the GPIO connector.The first project is a simple 7-segment LEDdisplay that displays the Raspberry Pi'ssystem time. The second is a roving robot thatuses the low cost Magician Chassis rover kit,along with the RaspiRobotBoard interfaceboard.
All the source code from the book is availableas a download from the book's website.
The book is available from most major booksellers from the end of November 2012 andfurther details can be found at the book'swebsite (www.raspberrypibook.com).
17
7/28/2019 The MagPi 2012 12 Issue 8
20/34
A n e x a m p l e l a s e r c u t p a r t
J o n a n d P a u l f r o m P i m o r o n i
T h e P i b o w g u y s g i v e u s a n i n s i g h t i n t o t h e i n n e r w o r k i n g s o f t h e i r f a c t o r y , h o w t h e
P i b o w h a s a f f e c t e d t h e m , w h a t p l a n s t h e y h a v e i n s t o r e f o r u s a n d e v e n t i p s o n
s t a r t i n g y o u r o w n b u i s n e s s .
p i b o w i n t e r v i e w
M e e t i n g t h e p i b o w g u y s w a s a n
i n t e r e s t i n g e x p e r i e n c e f o r m e a n d a s t h e y
w e r e p r e t t y c l o s e t o m e I t h o u g h t i t w a s
l o g i c a l t o g o v i s i t t h e m a s w e l l a s d o i n g
a n i n t e r v i e w , a n d I ' m g l a d I d i d !
Q : T o w h a t d o y o u a t t r i b u t e t h e s u c c e s s o f t h e
P i b o w a n d h o w h a s i t a f f e c t e d y o u ?
" P i b o w w a s j u s t s o d i f f e r e n t t o o t h e r c a s e s
w h i c h m e a n t i t r e a l l y s t o o d o u t . O b v i o u s l y
b e i n g f e a t u r e d o n t h e R a s p b e r r y P i . o r g
h o m e p a g e w a s a m a s s i v e c o u p f o r u s a n d
d r o v e a l o t o f t h e i n i t i a l i n t e r e s t i n P i b o w , f r o m
t h a t w e j u s t h a d t o m a k e s u r e w e s h i p p e d a s
f a s t a s w e c o u l d . I t w a s g r e a t s e n d i n g o u t b i g
b a t c h e s o f u n i t s , t h e n e x t d a y t h e r e w o u l d
a l w a y s b e l o a d s o f e x c i t e d a n d h a p p y t w e e t s
f r o m p e o p l e w h o ' s P i b o w h a d j u s t a r r i v e d . "
Q : I s t h e r e a n y t h i n g y o u w o u l d h a v e d o n e
d i f f e r e n t l y ?
" I f w e ' d k n o w n h o w s u c c e s s f u l i t w o u l d b e
w e ' d h a v e b o u g h t t h e e x t r a l a s e r c u t t e r s
s o o n e r w h i c h w o u l d h a v e m a d e t h i n g s a l o t
e a s i e r a n d q u i c k e r . T h e y a r e e x p e n s i v e
t h o u g h s o w e a l w a y s w a n t e d t o b e s u r e w e ' d
n e e d t h e m i n t h e l o n g e r t e r m . "
Q : W h a t a r e y o u r p l a n s f o r t h e f u t u r e ; W i l l t h e
r e b e a n y v a r i a t i o n o n t h e c o l o u r s l i k e a
s p e c i a l e d i t i o n ' P i w a s p ' ( b l a c k a n d y e l l o w ) o r
' A m e r i c a n a s t y l e ' ( r e d , w h i t e a n d b l u e ) , w o u l d
t h e r e b e c a s e s w i t h w h e e l s ?
" R i g h t n o w w e a r e f o c u s e d o n P i c a d e
( h t t p : / / w w w . k i c k s t a r t e r . c o m / p r o j e c t s / p i m o r o n i /
p i c a d e - t h e - a r c a d e - c a b i n e t - k i t - f o r - y o u r -
r a s p b e r r y - p ) w h i c h w e r e v e a l e d t w o w e e k s
a g o v i a K i c k S t a r t e r . T h e r e a r e s o m e p i b o w
t h i n g s c o m i n g i n t h e v e r y n e a r f u t u r e t o o -
i n c l u d i n g n e w c o l o u r s ! "
[ O . K , s o t h e c a s e s w i t h w h e e l s s u g g e s t i o n
w a s a l i t t l e f a r - f e t c h e d b u t w i t h h o w t h e s e
g u y s a r e p r o g r e s s i n g , w h o k n o w s ! ]
8
7/28/2019 The MagPi 2012 12 Issue 8
21/34
Q : F i n a l l y , w h a t a d v i c e w o u l d y o u g i v e t o
b u d d i n g e n t r e p r e n e u r s f r o m y o u r e x p e r i e n c e ?
" K n o w y o u r s t u f f . T h e i n t e r n e t i s f u l l o f
q u a l i t y i n f o m a t i o n i f y o u h u n t f o r i t . T h e n j u s t
g o f o r i t a n d a c c e p t a s m u c h h e l p a s f r i e n d s
a n d f a m i l y w i l l o f f e r : - )
W e ' v e w o r k e d r e a l l y r e a l l y h a r d o v e r t h e
p a s t f o u r m o n t h s t o s h i p o u t a l l t h e P i b o w s
p e o p l e h a v e o r d e r e d . W e ' v e r u n t h e l a s e r s f o r
a r o u n d 1 6 h o u r s a d a y , e v e r y d a y , i n c l u d i n g a l l
w e e k e n d s d u r i n g t h a t t i m e . W e w e r e p r e p a r e d
t o p u t o u r l i v e s o n h o l d t o e n s u r e w e c o u l d
d e l i v e r a s q u i c k l y a s p o s s i b l e .
F r i e n d s a n d f a m i l y h a v e b e e n a m a z i n g a n d
o f f e r e d h e l p a t t h e w o r k s h o p a n d a l s o
p r o v i d e d s o m e f i n a n c i a l s u p p o r t t o g e t u s u p
a n d r u n n i n g . W e c a n ' t t h a n k t h e m a l l e n o u g h
f o r w h a t t h e y h a v e d o n e t o h e l p . "
T h e y l o o k l i k e s o m e g r e a t c a s e s . H o p e f u l l y ,
w e w i l l s e e t h e P i c a d e k i t i n m a s s p r o d u c t i o n
s o o n t o o .
A r t i c l e b y C h r i s S t a g g
P i B o w s w a i t i n g t o b e s h i p p e d
D I D Y O U
K N O W ?
P a u l f r o m P i m o r o n i i s r e s p o n s i b l e f o r
d e s i g n i n g t h e R a s p b e r r y P i l o g o !
T h e P i B o w c a s e i s m a d e f r o m a c r y l i c .
P i m o r o n i n o w h a v e 3 l a s e r c u t t e r s c a l l e d
B e r t , E r n i e a n d C o o k i e M o n s t e r ! [ E d : I t
c a n ' t b e l o n g b e f o r e O s c a r , B i g B i r d a n d
T h e C o u n t a p p e a r . ]
1 9
7/28/2019 The MagPi 2012 12 Issue 8
22/34
CESIL - Standing for Computer Education inSchools Instructional Language was designedin the 1970's as an attempt to introduce youngpeople in schools into the world of computerprogramming. Without computers in schools,pupils wrote programs on paper and sent
them into their local computer centre. Theresults would come back in the post a weeklater!
CESIL is a very simplified assembly languagewith a very limited application base, however itis easy to learn and write simple programs in.On it's own CESIL is not terribly exciting, soI've written an interpreter for it in BASIC, andadded on a Christmas tree with programmablefairy lights! The tree has 4 rows of 8 lamps.
Think of it as a grid 8 wide and 4 high.
A CESIL program is essentially three columnsof text. The first column (which can be blank)is the label - it's a placeholder in the programwhich you can "jump" to from other parts of theprogram. The middle column is the operator -that's the instruction to execute, and the finalcolumn is the operand - this is data for theinstruction to use. This data may be the nameof a label if it's a jump instruction, it may be anumber or it may refer to a named memorystore, or variable.
My extensions to the CESIL machine haveincluded two more registers (three in total) tohold the row and column locations of thelamps and a colour instruction to set the lampcolour as well as a subroutine facility. Theprogram can be up to 256 lines and contain upto 256 variables.
The best way to explain it may be to look at anactual program. This program reads in anumber from the keyboard and prints a
multiplication table:
# mtable:
# Multiplication table generator
line
print "Multiplication table generator"
line
print "What table"
in
store table
load 1
store index # Index times ....
loop: load index
out
print " TIMES "
load table
outprint " = "
mul index # Table was in accumulator
out
line
load index # Add 1 to the index
add 1
store index
sub 11 # Subtract 11 counting 1 to 10
jineg loop # If
7/28/2019 The MagPi 2012 12 Issue 8
23/34
JINEG - Jump if the accumulator is negativeJIZERO - Jump if the accumulator is zeroADD - Add a value to the accumulatorSUB - Subtract from the accumulatorMUL - Multiply the accumulator with the valueDIV - Divide the accumulator with the valueHALT - End programIN - Read a number from the keyboard
OUT - Outputs the accumulator as a numberPRINT - Prints a literal string (in "quotes")LINE - Prints a new line
Extensions:
JSR - Jump to subroutineRET - Return from subroutine (to the line
after the last JSR instruction)
Christmas Tree extensions:
TREE - Build a new Christmas TreeROW - Transfer the accumulator into the Row
registerCOL - Transfer the accumulator into the
Column registerCOLOUR - Set the lamp indicated by the Row
and column registers to colour value in theaccumulator
WAIT - Delays for the given number of centi-seconds (100ths)
Note that you need to execute a WAITinstruction to actually reflect the colourchanges in the lights. That means that you canset a lot of lights at once, then when youexecute a WAIT (even a WAIT 0 instruction),all the lights will change at the same time.
There are 16 standard colours:
0: Off, 1: Navy, 2: Green, 3: Teal, 4: Maroon,5: Purple,6: Olive, 7: Silver, 8: Grey, 9: Blue, 10: Lime,11: Aqua,12: Red, 13: Pink, 14: Yellow, 15: White
Our Christmas tree has 4 rows of 8 lamps.Row 0 in the bottom and column 0 is the lefthand side.
The following program fragment will fill thebottom row with red lamps:
# Example program to light the bottom
# row with RED lights
tree # Make a tree!
load 0
row # R ow 0 - B ottom
load 7 # Count 7 to zero
loop:
store col-count
col
load 12 # Red
colour
load col-count
sub 1
jineg done # Jump If Negative
jump loop
done: wait 1 # Update the lights
halt
First download the RTB BASIC interpreterfromhttps://projects.drogon.net/return-to-basic/
Then you can install the CESIL interpreter anddemos, using:
cd
git clone git://git.drogon.net/cesil
cd cesil
rtb
load cesil
run
What I would like to see is people sharingexamples, so please post them on forums,email them to me ([email protected]) andwhat I'll do in January is to have a look at the
ones I've found and send a free RaspberryLadder board to the one I think is the mostoriginal, or clever...
Article by Gordon Henderson
21
7/28/2019 The MagPi 2012 12 Issue 8
24/34
C o m p i l i n g a n d R u n n i n g P r o g r a m s
O n c e y o u ' v e w r i t t e n y o u r p r o g r a m ( y o u m a y u s e a n y t e x t e d i t o r , s u c h a s n a n o o r G e a n y ) , s a v e i t a s
a . c p p f i l e , t h e n o p e n a n e w t e r m i n a l w i n d o w a n d t y p e t h e f o l l o w i n g :
g + + [ n a m e ] . c p p - o [ p r o g r a m _ n a m e ]
R e p l a c e [ n a m e ]
w i t h t h e n a m e o f t h e f i l e a n d [ p r o g r a m - n a m e ]
w i t h w h a t y o u ' d l i k e t h e a c t u a l
p r o g r a m t o b e c a l l e d .
I f y o u g e t a n e r r o r s a y i n g t h e f i l e i s n o t f o u n d , y o u w i l l h a v e t o u s e t h e c d c o m m a n d t o f i n d t h e
d i r e c t o r y y o u s a v e d t h e . c p p f i l e t o , a n d t h e n r u n t h e c o m m a n d a g a i n . I f y o u g e t a n e r r o r s a y i n g g + +
i s n o t f o u n d ( t h i s s h o u l d n ' t b e t h e c a s e o n n e w e r i m a g e s ) , t y p e s u d o a p t - g e t i n s t a l l
b u i l d - e s s e n t i a l t o i n s t a l l g + + .
W h e n t h e c o m p i l i n g i s f i n i s h e d ( i t m i g h t t a k e a w h i l e f o r b i g g e r p r o g r a m s ) , s i m p l y t y p e :
. / [ p r o g r a m _ n a m e ]
W i t h [ p r o g r a m - n a m e ] b e i n g t h e s a m e a s t h e o n e y o u u s e d t o c o m p i l e . T h e p r o g r a m s h o u l d t h e n
r u n , a n d w h e n i t i s d o n e y o u w i l l b e r e t u r n e d t o t h e t e r m i n a l a s w i t h a n y o t h e r c o m m a n d l i n e
p r o g r a m .
M o r e V a r i a b l e T y p e s
L a s t t i m e w e l o o k e d a t t h e i n t v a r i a b l e t y p e , w h i c h l e t s u s s t o r e w h o l e n u m b e r s . O f c o u r s e , w e
m a y n e e d t o s t o r e m o r e t h a n t h i s , s o t h e r e a r e d i f f e r e n t v a r i a b l e t y p e s . T h e y a r e a s f o l l o w s .
T o m a k e a v a r i a b l e , s i m p l y t y p e t h e k i n d y o u w a n t , f o l l o w e d b y t h e n a m e y o u w a n t t o g i v e i t ,
W E L C O M E T O T H E
C + + C A C H E !
L a s t t i m e w e l o o k e d a t t h e v e r y b e g i n n i n g o f C + + a n d
w r i t i n g o u r f i r s t f e w p r o g r a m s . T o d a y w e ' l l c a r r y o n , a s w e l l
a s s h o w i n g y o u h o w t o r u n y o u r p r o g r a m s a n d s o m e m o r e
v a r i a b l e t y p e s .
2
7/28/2019 The MagPi 2012 12 Issue 8
25/34
f o l l o w e d b y a s e m i c o l o n . H a v e a l o o k a t t h e c o d e b e l o w :
# i n c l u d e < s t r i n g >
# i n c l u d e < i o s t r e a m >
u s i n g n a m e s p a c e s t d ;
i n t m a i n ( )
{
/ / M a k e s o m e n u m b e r v a r i a b l e s :
i n t w h o l e N u m b e r = 5 ;
f l o a t d e c i m a l N u m b e r = 5 . 5 ;
/ / M a k e s o m e l e t t e r v a r i a b l e s :
/ / N o t i c e h o w w e u s e s i n g l e q u o t e s
/ / f o r c h a r a c t e r s , a n d d o u b l e q u o t e s
/ / f o r s t r i n g s .
s t r i n g g r e e t i n g = " H e l l o t h e r e " ;
c h a r p u n c t u a t i o n = ' ! ' ;
/ / M a k e a b o o l e a n v a r i a b l e :
b o o l i s T r u e = f a l s e ;
/ / P r i n t o u r v a r i a b l e s a d d e d t o g e t h e r :
c o u t < < w h o l e N u m b e r + d e c i m a l N u m b e r < < e n d l ;
c o u t < < g r e e t i n g + p u n c t u a t i o n < < e n d l ;
c o u t < < i s T r u e < < e n d l ;
r e t u r n 0 ;
}
N o t i c e w h a t h a p p e n s w h e n w e a d d t h e d i f f e r e n t k i n d s o f v a r i a b l e s . T h e n u m b e r o n e s w o r k a s
e x p e c t e d , a s 5 + 5 . 5 e q u a l s 1 0 . 5 , a n d t h a t ' s w h a t w e g e t o u t . W h e n w e a d d l e t t e r o n e s , t h e l e t t e r s
g e t a d d e d t o g e t h e r . S o w e h a v e o u r s t r i n g , H e l l o t h e r e , a n d w e a d d t h e e x c l a m a t i o n m a r k , s o w e
e n d u p w i t h H e l l o t h e r e ! . T h i s i s w h y d a t a t y p e s a r e i m p o r t a n t . I f w e h a d s a v e d t h e n u m b e r s a s
s t r i n g s , i t w o u l d h a v e j o i n e d 5 . 5 t o t h e e n d o f 5 , a n d w e ' d e n d u p w i t h 5 5 . 5 . I t i s a l s o a g o o d i d e a
t o g i v e v a r i a b l e s s e n s i b l e n a m e s . V a r i a b l e n a m e s c a n h a v e l e t t e r s , n u m b e r s a n d u n d e r s c o r e s , b u t
c a n n o t b e g i n w i t h a n u m b e r . T h e r e a r e a l s o c e r t a i n k e y w o r d s y o u c a n n o t u s e a s t h e y a r e r e s e r v e d
f o r t h e l a n g u a g e .
A l s o n o t i c e w e g e t 0 i n s t e a d o f f a l s e f o r i s T r u e . A b o o l e a n i s b a s i c a l l y a 0 f o r f a l s e , o r a 1 f o r
t r u e , j u s t l i k e b i n a r y , a n d s o t h a t ' s h o w t h e p r o g r a m o u t p u t s i t . L a s t t i m e w e u s e d c i n t o l e t t h e
u s e r i n p u t a v a l u e , a n d s t o r e t h a t a s a v a r i a b l e . T r y d o i n g t h a t w i t h t h e c o d e a b o v e . W e c a n a l s o
u s e t h e = s i g n t o c h a n g e w h a t ' s s t o r e d i n s i d e a v a r i a b l e a t a n y t i m e ( t h a t ' s w h y t h e y ' r e c a l l e d
v a r i a b l e s ) . T h e v a l u e o f o n e v a r i a b l e c a n b e a s s i g n e d t o a n o t h e r v a r i a b l e , f o r e x a m p l e :
i n t i = 4 ;
i n t j = i ;
H o w e v e r t h i s c a n n o t b e u s e d t o a s s i g n a v a l u e t o a s t r i n g :
i n t i = 4 ;
s t r i n g s = i ;
s i n c e t h e s t r i n g c l a s s h a s n o m e m b e r f u n c t i o n t o a l l o w t h i s a s s i g n m e n t . T h e a s s i g n m e n t o f a
n u m b e r t o a s t r i n g c a n b e a c h i e v e d w i t h a s t r i n g s t r e a m , w h i c h w i l l b e d i s c u s s e d i n l a t e r t u t o r i a l s .
A r t i c l e b y A l e x K e r r
2 3
7/28/2019 The MagPi 2012 12 Issue 8
26/34
B I G W O R L D
B a b y s t e p s i n a . . .
A d a , a l a n g u a g e f o r e v e r y o n e
B y L uk e A . Guest
I n t r o d u c t i o n
F o l l o w i n g o n f r o m i s s u e 6 , w e w i l l c o n t i n u e t o
c o v e r t h e b a s i c s o f t h e A d a l a n g u a g e .
N u m e r i c T y p e s ( c o n t i n u e d )
I n t e g e r t y p e s c a n h a v e n e g a t i v e n u m b e r s , s u c h
a s - 1 0 , - 5 5 , e t c . N a t u r a l t y p e s c a n o n l y a c c e p t
v a l u e s s t a r t i n g f r o m 0 ( s o , n o n e g a t i v e n u m b e r s )
a n d P o s i t i v e t y p e s c a n o n l y a c c e p t v a l u e s
s t a r t i n g f r o m 1 ( n o n e g a t i v e v a l u e s a n d n o z e r o
e i t h e r ) .
A l l o f t h e s e t y p e s c a n b e u s e d t o g e t h e r i n
m a t h e m a t i c a l e x p r e s s i o n s , b u t y o u m u s t m a k e
s u r e y o u d o n o t g o o u t s i d e o f t h e r a n g e o f t h e
t y p e y o u a s s i g n t o , o t h e r w i s e a n e r r o r w i l l o c c u r .
F o r e x a m p l e , i f y o u d e f i n e t w o v a r i a b l e s X :
N a t u r a l : = 1 ; a n d Y : I n t e g e r : = 2 ; a n d t h e n
s u b t r a c t Y f r o m X a n d t h e n a s s i g n i n g b a c k i n t o
X a g a i n ( e . g . X : = X Y ) , t h i s w i l l c a u s e a n e r r o r
a s t h e r e s u l t i s - 1 w h i c h i s o u t s i d e t h e r a n g e
a l l o w e d f o r N a t u r a l t y p e s .
B o o l e a n t y p e s
B o o l e a n t y p e s h a v e t w o v a l u e s , T r u e a n d F a l s e ,
t h e r e i s n o t h i n g e l s e y o u c a n a s s i g n t o a v a r i a b l e
o f B o o l e a n t y p e .
S i m p l e d e c i s i o n s
A l l l a n g u a g e s h a v e t h e i d e a o f b o o l e a n v a l u e s a s
a l l c o n d i t i o n s i n p r o g r a m m i n g r e l y o n t h e i d e a o f
s o m e t h i n g b e i n g t r u e o r f a l s e .
T y p e i n t h e c o d e i n L i s t i n g 1 t o s e e h o w w e c a n
Line 5 : W e d e f i n e a B o o l e a n v a r i a b l