hello my name is Simon Munk and I'm
going to be giving you a quick sort of
whirlwind tour of programming the
BeagleBone black using bone script my
job is writing books about open-source
hardware technology so I have books
about the BeagleBone raspberry pi the
Arduino and also some general electronic
texts as well so the BeagleBone black if
you've I'm sure you've seen there's a
lot of interest in it in the BeagleBone
black this year the this little board I
think they're about $45 something like
that looks superficially a little bit
like an Arduino board or a Raspberry Pi
board and in fact it's got things in
common with both of them it's a single
board Linux computer it's got it at the
moment the the the versions that are out
there already which are revision 1 and
revision to use the distribution of
Linux called am strong
the newest distribution that the newest
revision revision see which is just
starting to ship uses Debian Linux
rather than and strong both they were
these devices will actually can either
boot directly from a memory chip a flash
memory chip on the actual board or you
can plug an SD card micro SD card into
the end here and then you can have it
either transfer the contents of that
micro SD onto the main flash on the
board and it kind of flash the entire
board to a new distribution or you can
have it run directly off the micro SD
card you get a choice in that respect so
if you look at some of the features of
the BeagleBone black you'll see that
we've got some one thing that makes it
different from other single board
computers is that we've got lots of GPIO
connection
so you can attach electronic hardware to
the BeagleBone black using these two big
connectors here to double rows of
sockets that you can plug your
electronics into some of the other
things to point out on on the board are
an Ethernet jack so that you can plug it
into your network at the opposite end we
have a USB host socket so you can plug
keyboard and mice on into there so you
might want to attach a USB hub and then
connects up all your USB devices or you
might want to plug a Wi-Fi adapter in so
that you can actually take the whole
board mobile and have it connected to
your network in a mobile manner which is
of course a good way if you were
building using this to say the
controller for a robot and then you
could send commands it could be a web
server and you could send commands to it
it has a micro HDMI output so for that
you would need to connect up a little
adapter and then you'd be able to plug
in plug it into your monitor or TV so
you really can set up an entire home
Linux system using this little board
okay when it comes to programming the
BeagleBone black the sort of interest
that I have in this is for programming
it to control electronics in the same
sort of way as you would in Arduino and
for that that choice is really our that
you can use Python which is a very
common programming language lots of kids
are learning to use Python at school you
can also program it in C and you can
program it in bone scripts and bone
scripts is kind of the as much as
anything is the standard language for
programming the people both like if you
go to the BeagleBoard got all websites
once you've bought your new board and
you've plugged it in you'll find
instructions there that lead you through
using bone scripts
it's a program the BeagleBone black so
this talk is going to be about using
bone script so what is bone script well
too so that question we really need to
know a little bit more about how it's
all how all the software is stacked up
on this device so we have Java scripts
which is originally a browser language
and it's often used in websites so that
you can do things like if you know you
can have countdown timers that's a
implemented that run actually on a
browser okay however javascript is not
really intended for is not originally a
design for running on a server and when
you're running JavaScript off the
browser in other words on a server or
some other kind of machine like a
BeagleBone then you need use a variation
of JavaScript called node nodejs
to get to give it its proper name so
bone scripts the actual language we're
really going to talk about is a library
for controlling the hardware the byberry
for turning these input/output pins on
and off saying whether they should be
inputs or outputs reading analog values
from them all those sorts of things it's
a library for doing that in JavaScript
on No
yes and the way that you connect to a
BeagleBone black when you want to
program it is that you actually the
Beagle brown bone black when you turn it
on soon as you plug it into USB that
will power it up and it will also start
it running as a little webserver and not
just a little web server that serves a
bit of content about the BeagleBone
black but also a web server that that
posts a web based integrated development
environment which is both Clapperton
and this cloud9 IDE allows you to write
programs actually on the BeagleBone
black and then store them on the
BeagleBone black file system all through
a web interface so your way of
communicating with the BeagleBone black
is to use a browser on your laptop for
example so let's just I'm going to start
a little demo up just to demonstrate
that so I come out of the presentation
and if we go over to a browser we can
see here that this this browser if you
can see if you can just about read the
URL bar at the top it says 192.168 7.2
and then Co long 3000 because it's
running on port 3000 and this your this
web address here is actually a it's
using the USB connection as a sort of
pseudo network connection to the
BeagleBone black so you don't even have
to plug your BeagleBone black into into
your home hub or your into your local
network you can actually connect to it
as a network device through the USB
connection that's also powering it so
this webpage here is actually showing a
page from the integrated development
environment that's being served up by
the BeagleBone black so on the left hand
side of the screen we have a sort of our
project explorer where you can see all
the different files that that are
available and you can see
organized the folder here called Bay
Area make effete 2014 and then here I've
got the programs that I'm going to run
in this demonstration so one of these is
already loaded up here and it's called
it's actually called the blink blink
built in oh the bigger board black has a
number of built-in LEDs actually on the
board and you can control them
programmatically as well as being able
to control all the GPIO pins on these
connectors so let's just run this
program and to run it I'm just going to
click on the Run button here but if I
could just ask the control desk could
you switch over to show the show the
desktop please could you switch thank
you
okay so you can probably see we've got
the bigger board here and the area to
look at is just over here by the side of
the Ethernet connector and I'll run the
program and can you see the LED blinking
on there okay good it's not that
exciting is it it's more exciting when
you attach some extra hardware to it so
let's just go and see how that code
works to get it to do that so if we
could switch back to the computer please
and I'll go back to the presentation
okay
slideshow play from current slide so
here's the the code that that does that
that makes the LEDs blink and if you're
if you've done any JavaScript then
you'll follow this fairly easily if you
haven't done JavaScript then it'll
probably all look a bit mysterious and a
bit unusual particularly if you're sort
of used to an Arduino background which
does things in rather a different way so
if we start at the costs at the top of
the code we see we've got this VAR b
equals require bone script this is how
you import a library into into into
JavaScript and then on the next line
we've got a variable called LED and
we've assigned that the string user 3 us
r3 so that refers to this LED on here
that's blinking you have four LEDs us us
r0 to us r3 that you can control there
so if we wanted to turn one of the i/o
pins on on here on and off instead and
I'll show you that in a minute then we
would just use a different value in
there instead of the LED on the next
line we are specifying that we want that
LED pin to be an output so we do that by
we've got this handle be this variable B
which is the bone strip library so we're
basically saying telling the bone script
library make the pin mode of the LED
be an output we then have a variable
here that we use to keep track of
whether the LED is on or on or off and
then we have a function here that
toggles the LED so what that does is it
takes the state variable and if it's
initially 1 it turns it to be sitting
arrow if it's 0 it turns it to be 1 so
it inverts the value of this variable
and then finally inside this function we
have B so both stripped digital rights
the LED to whatever the value is of
state so either one turn the LED on 0
turn the LED off so we put all of this
in a function but what we now need to be
able to do is to be able to run that
function every half a second to make the
LED toggle between being on and off
every half second and we do that with a
JavaScript thing called an interval
timer so we do set interval then we
specify the function that we want to run
which is called toggle LED and then we
specify the period in milliseconds that
it should be run so this will run toggle
LED every half second blinking the LED
on or off ok so that was the built-in
LED not terribly exciting exciting if we
wanted to connect an external LED then
we could do it like this we've just got
here a bit of breadboard we've got we've
got a ground connection that's the blue
wire coming from the one of the
connectors on the beep Bo black and then
we've got an LED in series with a
current limiting resistor connected by
this orange lead so one of the GPIO pins
of the BeagleBone black so not going to
show you that today but I'm going to
show you something a little bit more
exciting which is we don't have to just
control an LED we
you know in a way and LEDs not that
exciting a thing to control a big
old-fashioned light bulb of the sort
that you'd get in the in the back in the
tail light of your car it's a bit more
interesting but it takes too much
current to be able to directly drive
what takes a lot too much current to be
able to directly drive a bulb like this
from one of the connectors on the
BeagleBone
so we use a transistor which is this
thing here to actually amplify the
currents and allow it to switch the bulb
on or off using an external power supply
so we're going to use this little
battery pack to control the LEDs so you
can see on the the diagram there how
this is wired up there's a transistor
there's a little resistor that limits
the current going into the transistor
and then the transistor is basically
switching the currents of the bowl that
is supplied through this battery
connector here this is all built on one
of these ADA fruits
boards that you can buy as a kit and
they're intended for prototyping so they
just have that you just sold it at the
connection pins and then it will plug on
top of the BeagleBone black like that
and then you you solder your own
components in this middle area here so
if you want to build a simple prototype
to do something like this then this is a
great way of doing that these these
shields are about fair unix expensive to
buy so let's connect that up and have a
little do a little demonstration so I'll
switch back to the development
environment which is of course on our
browser and I'll close that program and
I'm going to fetch up another program
called blink light bulb so you can see
the program is almost identical the only
thing that's really changed is the pin
that we're using for the LED so we've
changed that to be p9 underscore 14 so
the BeagleBoard has two rows of
connectors one called p9 and one called
p8 and then the individual connections
of them is the bitter after the
underscore so we're using p9 underscore
14 which is one of the ones roundabout
here to actually control the transistor
that's going to control the light bulb
and turn it on and off so we'll just
wire this up now so I'll pop this on
here right now plug in the power supply
okay so now when we run the program stop
the old program running run the new
program okay so a bit more exciting
could we just switch to the think Ramona
so there we go
a light bulb although this is using a
light bulb there is no reason why you
couldn't use something more
sophisticated that so you could use a
relay say that controlled a big motor or
anything or a mechanical door lock or
something like that so the basic
principle is you can use a transistor
control quite high current from one of
these little GPIO pins okay if we could
switch back to the presentation please
thanks to the thanks
so let's continue with that so what
we've been doing so far is digital
outputs we're changing a pin on the
connector here to be from being on to
being off they're digital outputs the
pins on the BeagleBone black capable of
a lot more than that you can use them as
digital outputs but you can also use
them as analog as digital inputs so you
can connect switches to the BeagleBone
black and detects whether the switch has
been pressed or not in your programs you
can use analog inputs so you can measure
voltages and then do something with
those voltages and you know display them
for example so this is a pin out it's
probably too small to see properly on
the screen but you'll get an idea that
there are lots of pins available to you
them most of them can be used as digital
inputs and outputs some of them can be
used as analog outputs and some of them
can be used as analog inputs so a real
mixture there as an example if we were
going to use digital inputs then we
might want to say wire up a couple of
switches and an LED like this so you can
see that the if you look follow the blue
wire first you'll see there's a blue
wire that comes out of the BeagleBoard
and this is the ground wire it's the
sort of common reference 0 voltage the
ground wire and it's connected by this
bus wire down the side of the breadboard
to the bottom side of one switch and the
bottom side of the other switch okay so
each of the switches at the top end of a
switch then is connected to a Jeep one
of the GPIO pins you'll notice that
there's also a little resistor here this
for each of the switches that actually
connects that same top end of the switch
there's connected to the GPIO pin it the
resistor bridges that up to the positive
supply voltage the 3.3 volts supply on
the big award and the
is called a pull-up resistor and the
idea is that when the switch isn't
pressed the input is all permanently
being pulled up to 3 volts and then when
you do press the switch it effectively
switches that shorts that input down to
ground and the and the input value is
then at 0 volts okay so you have to use
this kind of approach with a pull-up
resistor otherwise the input could float
and it could sit at some indeterminate
States and you don't know whether it's a
1 or a 0 and if it picks up electrical
noise it could switch between the two
and we're very confusing the code for
doing digital inputs is very similar to
the code we saw before for doing a
digital output you first of all have to
import the burn strip library so we do
the B equals require and then we've got
this variable B that's a reference to
the library so every time we want to do
something with the library we have to
save B dots and then the command to do
something with it
we've got a pin variable that we're
going to use for the switch pin that we
set C is going to be p9 12 and this time
we do B dot pin mode pin B dot input so
we're specifying that that that pin is
an input we've then got another function
here called read input and then what
that what we have here is that it takes
it uses another variable that reads a
value from the digital inputs from the
pin specified in our variable up here
and that reading will either be 1 or 0
it would be on or off true or false and
then finally to repeatedly read the
input then we've got that we're using an
instable timer so it sets interval so
once the second it's going to read the
input and check it that's not completely
satisfactory because what happens if
you're using a button then you chrisley
quickly press it and it's gone down and
back up again before it's had time to
register it so there are more advanced
features that are something called
interrupts there's a that essentially
mean that the function will only
actually get run when the button is
but it does rather depend what type of
input you're using whether you want to
have it trigger into an interrupt or
whether you just want to keep what's
called polling and checking with the
value of the and a lot of the digital
input okay
so that's enough about digital inputs of
this kind of introductory stage analog
inputs one of the big differences
between the BeagleBone and the Raspberry
Pi is that the Raspberry Pi doesn't have
any analog inputs if you want to connect
analog devices to it you have to have
you do some special tricks it can't
directly measure voltages you can tell
you whether a voltages of below baltha
or below a threshold so it can just say
is it a one or is it a zero is it above
one point 6 volts or below one point 6
volts but it can't say measure the
voltage and say yup that's a that's
point eight of a volt or that's 1.2
volts or whatever but the BeagleBone
black does have these analog inputs so
you might want to connect up a light
sensor so what we've got here is we've
got a photo resistor that's what this
thing is here and we've got a fixed
value resistor here and these are
arranged in what's called a potential
divider so we've got the top end of the
photo resistor is connected to a
positive supply the where the variable
where the photo resistor and the fixed
value resists the meet is connected to
one of the analog inputs of the
BeagleBone and the bottom of the
resistor this blue wire here is
connected to a ground connection
of the BeagleBone black so if you're
used to it
Arduino then be a little bit careful
when you come to use the BeagleBone
black because the Arduino will have will
has analog inputs that are 5 volts you
can put any voltage in up to 5 volts the
BeagleBone black the maximum voltage
that will read is 1.8 volts so you have
to be careful not to exceed that as well
as providing I think 6 analog inputs
they'll measure a voltage between 0 and
1 point 8 volts it also provides
high-quality ground and 1.8 volt
references so that if you're doing an
arrangement like this where you're using
where you you're using some kind of
potential divider you know that you're
getting an accurate supply for the top
end of what you're measuring and by
using these special analog reference
pins that are available on the GPIO
connector the code for reading analog
inputs is a little bit different from
what we've been looked at so far so
again we require the bone strip library
and then here we've got a function
called read lights and we're just going
to call read light on this interval
timer down here so that every half a
second it's going to call this function
but there's something interesting going
on here because if you look inside this
function it's all it does is the booby
dots analog read it off' eyes a pin to
use so P 939 with one of the analog
input pins and then this thing here is
the name of a function because the
analog read could potentially take a
little bit of time the JavaScript way of
doing things is that you use callbacks
so what we're basically saying it to the
BeagleBone here is go ahead and take an
analog reading and when you finish
taking that reading call this function
called display light and the display
light function is what's actually going
to oh sorry about that
it's what's actually going to print out
a value for us for the lie
intensity so when this callback happens
and display light gets called it gets
passed a parameter which is the reading
we do a bit of maths on that to turn
that into a voltage and then this line
that says console dot log we'll just
write out the voltage on the on the
command line for us so that we can see
the reading you might want to do other
things like control the brightness of a
light or if it the reading that you get
is above a certain threshold you might
want to turn on a digital output so
let's say you had a temperature sensor
read the temperature if it's above a
certain amount it might flip a relay
that turns on the air-conditioning for
example to make yourself a simple
thermostat okay so that's analog inputs
well as analog inputs the BeagleBone
black can also do different types of
pulsed outputs which are rather
equivalent to analog outputs so you can
do PWM which stands for pulse width
modulation which is all about
controlling power so you send out little
kicks of power that would then control
the speed of a motor or the brightness
of a light bulb that kind of thing
the other kind of pulsed outputs that
you can get from a quite a big range of
the pins on the BeagleBone black is
called pulse code modulation which is
what you use for setting the position of
a servo arm so you can control a number
of servo motors directly from the from a
BeagleBone black using a digital output
so just having a looking very quickly
that's a pulse width modulation side of
things controlling the power it's very
straightforward again you have to
include the bone strip library and then
you just do B dot analog right and then
you specify the pin that you want to
write and then this value here is
between 0 and 1 so 0 means no power at
all and if it's one that means full
power so that the the device will be
we're receiving the whole of the duty
cycle and being completely on
okay so to summarize using this kind of
bones using the bone script language is
quite a different approach from Python
or see that you'd use if you're using an
Arduino you use callbacks so that when
something has happened like an analog
reading has been completed it makes some
further action it calls another function
in code and you also use timers so you
use these events Hymas so whereas in an
Arduino or in Python you might delay or
sleep for half a second and then move on
some kind of code in a strict bone
script you actually set a time and to
say do something at this period and we
have this nice web-based integrated
development environment that lets you
actually program the BeagleBone black
using a web interface that's running on
the BeagleBone itself which is a very
nice touch ok if you want to see a more
advanced session that looks at using
controlling the BeagleBone black using a
web interface then i'm here again
tomorrow
please check the time because I'm not
actually absolutely sure that that time
is correct I know there's been a few
changes that are scheduled but I'll be
here they're here again they're talking
about that and take a look at the
official big awards site there's loads
of really good information on there and
of course I have to say my book which is
now programming the BeagleBone black by
by me salmon run all the code that I've
been describing here is also available
on my github site so if you go to github
and you look in Simon Munk you'll find a
number of BeagleBone black related
repositories there where you can pick
out the code that I've been using today
ok that's it I think we have a few
seconds for any questions or anything