The Ultimate Arduino Bootstrap

Preben Thorø

Recorded at GOTO 2016

The Ultimate Arduino Bootstrap

Preben Thorø

Recorded at GOTO 2016


Get notified about Preben Thorø

Sign up to a email when Preben Thorø publishes a new video

thank you so I have 2 plus 15 years this
is how far we can take it talking about
15 years we're not going to talk about
me but does any of you recognize this
it's kind of talking about me because
this is talking about my age this is the
Commodore 64 which we used to play with
when I was a teenager and when I think
of what we could do what kind of magic
we could do with this thing I can't help
thinking of what could we achieve if we
had the possibilities that we have today
and the time we had back then and this
is kind of what drives this talk because
I wanted to show one of the easiest way
to make digital products and there's a
predefined overlap with that and arduino
because what originally defined the
arduino project was what the wiring
platform done by a colombian student in
italy back in 2004 he wanted to make a
little package with hardware and an IDE
so that everyone could make digital
products one year later someone took his
work replaced a couple of a bit of the
chipset with a cheaper chipset and open
sourced everything and that was the
birth of Arduino and by the way the name
Arduino that is that happens to be the
name of the cafe whether little crowd
met when they did thing back then
Arduino is open source and probably
because of that it has a huge community
and it's used throughout the world for
educational purposes and it's so so easy
to get started so let's start and we
really really really start with the
basics this is an arduino board in real
life it could look like this
we take a look at the of the other part
of the picture here we see a row with
some connectors some pins this is
digital i/o at the bottom we see analog
pins and then there's a USB connector
and a power connector just pretty up
it's a USB is for programming the thing
and as compared to the Commodore 64 that
lost everything when we turned off the
power this has flash memory so when we
upload the program so this thing it
stays which means then we don't need the
USB cord any more than we can power with
the power block afterwards Arduino is as
I mentioned it has a huge community and
you can find extensions for anything and
I really mean anything and if some of
you is thinking about some weird thing
for that yes you could you can find
extensions for anything we call him
shields you can find a shield for
anything with the Arduino board let's
get started this talk is going to be
good old plain code no rocket science
just code snippets please keep that in
mind with when you vote to program the
thing we download the IDE this is not a
real rich IDE as we know it from visual
studio or Xcode or in celle de or
whatever it's more like an advanced
editor with syntax highlighting and with
a compile button but this is what we
have let's see what we can do with it
let's make a hello world program and
somehow all hello words programs in the
arrow inner world control pin
13 and the reason for that is quite
simple pin number 13 is hooked on to a
little lamp on the PCB so it's pretty
easy to see if if the things work will
do the same thing here this code snippet
well this is our hello world at the same
time this shows the pattern for all
arduino boards all our we know programs
no exception to the rule everything
follows this pattern so let's set up
which surprise is being executed once
and then there's a loop and there's no
timing in the loop it's just being
executed over and over again and this is
what the idea we know can set up and
loop nothing else our little program
here well today we will work with pin
number 13 we can figure that remember
the upper row of the pins they were
digital input/output so today it's an
output we've configured that in the
setup method and in the loop we pull it
high wait a second low wait a second and
just because this is deep down symbol
let's see how it works so this is the
result marvelous let's see how far we
can take this let's make hello world
part number two if we take a look at
those connectors we see less and RX and
TX as a serial communication thing on
the board if we don't do anything with
these two pins they are through magic
hooked up to the USB port and the
Arduino IDE has a little terminal so we
can actually use that as a screen which
we do like this and it's pretty simple
to do so I just extend my hello world
here see the line in the setup syrah
begin with some port speed and down in
the loop I write something high and low
and we probably recognize the syntax
print and print with a line shift and if
I open up the terminal this will be the
output so we can actually do some
debugging to not only with a lamp that
we can turn on and off and as you may
have seen already now programming is see
this is C syntax in most cases you don't
have to think about say the more
hardware specific parts of C addressing
memory doing pointer dereferencing
pointers everything is in most cases
shield it away or kept in struts or well
some methods call ish things library
functions let's let's go one step
further the my screen has an input field
to my serial communication well it had
an n RX and the TX o of course we can do
things both both directions so this is
what we can do you know set up once
again we can figure the serial channel
and as long as we have something
available we read it and print it so
this little coat in fact does nothing
but whatever you type in the input field
is echoed in the output area all we can
do it a little bit more rich rather than
reading character by character we can
work with entire strings and this is
where parts of the sea libraries in the
Arduino gets a bit more advanced which
you actually work with strings not with
character buffers as you would do and
see there are libraries for everything
it is C but
something is sealed it away let's see
what else we can do it has inputs and in
the other we know worse you don't need
pull up a pulldown resistors and I know
only hardware nerds as I would know what
that means so let's just take a very
brief Logan see this very simple figure
here let's say we have an input on the
other we know and I want to do something
with that when that input goes low or
high but until then I don't want to do
anything now the thing is on this
microcontroller everything that has a
voltage below 1.45 is definitely low and
everything that has the voltage above
3.3 volts is definitely high but in
between you don't really know what it is
it could be anything so if I would make
an input that just reacts when we go low
or high and until then it just floats
it's not hooked up to anything I really
wouldn't know what state it would be if
it would be low or high so in this
situation until I close the switches I
actually don't know the electrical
potential of the input which is why
always when you're working with digital
i/o you use pull up or pulldown
resistors because when the input is
inactive through through that resist
that you can determine if it should be
hi hello always almost always because
this is so always let it has been done
for us in the Arduino world so dealing
with digital input you don't have to
solar resistors on to it let's see how
it works what does this program do well
in the set up that it defines a pin for
output then it defines another pin for
input and it writes that input hi
it pulls it high it mounts a pull-up
resistor to let and then down in the
loop I read the input and if the input
goes low i turn on my output or else I
turn off my output so in fact this
program it reads an input and if the
input is high the output is low if input
is low the output is I this is what we
do here stupid example let's see what
stupid question number one of today
sometimes the questions are so stupid
that you don't even dare to answer it it
turns on an output wait for five
milliseconds turns it off waits for five
the result here the result is that half
of the time the input is high and half
average it's somewhere in between and if
low corresponds to 0 voltage and if high
corresponds to five then this means that
on the average the output is 2.5 volts
we call this pulse width modulation and
this is in fact supported by the Arduino
so you can do analog output with the
Arduino if you take a look at some of
the pins you can see some of them have a
little mark those pins supports this
analog mode
it works like this so I define my output
any number between 0 and 255 defines the
analog level of the output ok or the
this is brilliant because now I can
realize part of my use case as you may
know our company the perfect app makes
some app tool this app tool can draw a
curve of the number of active apps out
there right now I want to make that
curve with a laser beam and for that I
need an analog signal I want to control
I want to have a mirror that can reflect
a laser beam and by moving that mirror I
can draw in one dimension if I could
have another mirror moving in the other
direction I could draw a laser beam in
two dimensions and for controlling a
laser beam that would be the thing and
don't laugh if you think of a
loudspeaker it's actually very very
accurate if you think of what it's able
to reproduce it has a very very fine
resolution so that would be the thing to
use but of course this is not some
brilliant idea that I just got here this
is pretty common thing that you want to
reflect a laser beam with a loudspeaker
in fact it's so common that you can buy
a thing like this this is in fact a loud
speaker meant for this for the purpose
so this is what I'm going to do I'm
going to use to analog outputs to
control those two mirrors to be able to
draw a laser beam in two dimensions on
the war and just to make everything very
clear this is the physics
let's make a little experiment here so
if I have a full range in po both
directions 0 to 5 volts that means for
my very first test if I can apply 2.5
volts to each coordinate the beam should
be in the middle of the total working
area right so in the Arduino world it
would look like this two analog outputs
and something in the middle of 0 and 255
as a value of each one of them let's do
so and this is my result I expected to
see a laser beam right in the middle of
the working area and this is what I got
so that leaves me with two choices one
it's goodbye to laser beam incorporated
or two I need to learn something more I
don't like any of those choices but if I
should pick let's learn something more
first of all why is it let me see this
well this is in fact because yes we
support some kind of analog signaling
with the Arduino board but the frequency
is very low it's actually at 500 Hertz
so what I had on the picture was
actually all the lows and all the highs
engine and then the movement between we
need to learn something more let's dive
further into the Arduino world analog
inputs there's a number of inputs named
Dave zero I think it starts with a0 to
something this is how we control them
they work this way that as signal
between 0 and 5 volts on that input pin
is converted to a number between 0 and
thousand and twenty three so look at
this
i define an input and one of those
outputs that can actually act as an
analog output I read the value of the
input using the map function I map the
value of the input to something that I
can print on the output pin and I print
it you know for lamps and everything it
would be perfectly fine to use those
analog outputs because you cannot see
that it flashes so quickly so this in
fact it reads the value of an analog
input and every reproduces that value to
the analog output these are the basics
digital i/o an analog i/o and frankly
everything we've seen so far we could
have done that with a wire and some
soldiering to its not very advanced
let's take it one step further this
little thing here it's a little
temperature measuring thing it works
this way you apply 0 to 1 pin plus 5 to
the other one and on the middle pin you
can read the voltage a voltage letters
rip that is dependent on the current
temperature of the thing or to put it
another way a voltage or the temperature
is reflected through a voltage between
20 and 5 volts it works like this so in
my loop I read the temperature the
temperature reads the value of the
analog input remember that's the value
between 0 and 10 24 I convert that into
the voltage I know that it has an offset
because it can actually measure negative
temperatures and forgive me I know where
scientists the probably is no such thing
as a negative temperature but basically
ringing searches yes
so I convert the voltage into a
temperature and I print that which looks
like this in this little example I have
my fingertips under thing so I heat I
try to heat it up with my fingertips so
you can see the temperature increases
and then I let go and the temperatures
start to fall again and look somehow
though I have a number of digits somehow
it fell back to the exact same
temperature as it reached on its way up
this kind of shows that yes it is an
analog input that we read but it has a
limited resolution a number between 0
and 1023 is in fact a 10 digit it's a 10
digit resolution 10 bit resolution this
is what we see here it is some kind of
limited does any of you know about the
pc open source project called LCD proc
that was around 10 years ago now we're
back to my age too yeah I can see you
know it yeah there was this this display
hitoshi display this is the most common
display in the world you see that
everywhere on the coffee machine on
whatever every time you see an LCD
display this is the thing this is a
standard back in the days there was a
project and open source project for
linux and windows where you could
control this display using the serial
port or the parallel port which were
common on pcs back then of course this
display is supported by arduino too and
it takes very very few lines of code to
actually use the display you can see on
the bird's nest here that i have a
couple of wires those wires basically
mount the data pimps to the display and
love controller signals and this is what
I do here I include the library liquid
crystal I make a variable define it with
the outputs that I intend to use today
and then I can print on the display this
is all it takes to print on a display
like this and the result goes like this
now we can start to do more advanced
things because now we can combine things
so in this case I read the temperature
as I did before and then I write it to
the display which reveals another thing
see it's actually more than a display
it's a screen it even has a cursor it
has a print position you can see that in
the middle of the slide here I actually
set my cursor position the print
position back and yes there is a space
after the string that I print because it
could be that i'm printing a shorter
string that I had before and then i
would like to clear what's left so in
this case I have a little thermometer
like this with like 12 lines of code and
a couple of wires let's move on here
think of serial communication what is
that well that is in fact just some
output digital output that you pull high
and low at a certain pattern and someone
else can recognize that pattern and then
we can communicate over serial channel
which means that all input outputs on
the board could actually be used for
serial communication this is what the
library software serial does it allows
you to use any two inputs for serial
communication and a lot of extensions a
lot of shields make use of that
one of those shields is that one here
this is the GPS I don't know if any of
you is familiar with the nmea standard
of GPS that's another way of putting
nonsense nmea this one the manufacturer
told me that you can expect this to
communicate on pins three and four so
use the software serial library and you
can communicate with this and this is
what I do here and doing very very
little but in the loop as long as the
GPS serial line here has something to
tell me I read it and I print it and by
a way please note line number five the
tiny gps + thing that will become
important later on but this one this
actually reads the stream coming from
the GPS here and this is what it gives
me this is in fact the raw data of an
enemy a TPS I have no idea what to look
for in these numbers sometimes I think I
recognize some coordinate there but I
don't this is where this tiny GPS
library comes in that you saw on the
previous slide using the tiny GPS
library I made this GPS object and will
that GPS look down here as long as I
have something to read I pass it on to
gps with the encode method here I do
list for some time and then I returned
to my main loop function and now I can
actually interpret what the thing had to
tell me so this is what it takes to
print a thing like this to actually read
a GPS with an Arduino and you can read
all kinds of things not only the
coordinates
lot of things let's move on here let's a
software project or library called wire
or the wire protocol on arduino using
that you can make something like a USB
bus with the arduino not serial
communication point-to-point but more
like a bus USB or canvas or whatever you
used to it has some limited speed and it
has some limited address space but you
can actually use that to change things
in the Arduino world the following
example here is stolen without
modification from the official arduino
work web page look at this in the loop
it starts to transmit to someone whoever
might be listening at address number
eight then it write something and then
it terminates the transmission and then
characters for whoever has address
number eight on the wire bus this one is
a bit different in the setup we tell the
wire protocol that now we want to listen
on a dress number eight then we register
some callback method which is being
called whenever something appears for a
dress number eight on the wire bus so
this is what we do here so this in fact
reads what was transmitted by the
previous program and prints it out on
the serial so we can chain to Arduino
BOTS two or more and the way we do it is
just by adding a few wires and please
note those wires are not crossed because
this is not point to point this is not
our X to TX this is
Pass serial bus communication this is
very good to know because then we can do
things with higher resolution than just
digital input/output I googled very
short and found this digital to analog
converter it's very small it's the size
of my thumbnail this little thing here I
want to use that to realize my use case
the vendor told me that two useless you
need to use the wire protocol then he
gave me the library this out of fruits
mcp something library and I can use that
so I set up two of these ports to
digital-to-analog converters the two
different addresses the vendor told me
that too you can expect those addresses
to work in the setup method and then I
make some coordinates and in my loop I
just continuously plot V X and the y
coordinates out on the wire bus using
the set voltage see everything has been
shielded away or kept away from me I
don't have to think of this as C they
just provide me the library and this is
what it takes to print out a value to
the little boat that I got there and
this is a result then now it starts to
look like something so now apparently I
can well I can hope that it would be
possible to realize at least the drawing
part of my use case but also need to
fetch the data out into my box here so
we need to learn something more
there are two places on the internet
where you can find almost anything for
arduino the one is out of fruit with was
mentioned on the previous slide with
that little PCB there's another location
sparkfun you can find anything there
this is a Wi-Fi shield they provide it's
pretty easy to use that Wi-Fi shield
because of course they provide a library
too so if we look start down here this
is actually what it takes to configure
so I could configure that to my network
and then I can start playing with it and
let's see what we can do see now that i
have it hooked on to the network I can
do TCP communication with that shield I
arrow points to see i connect to some
server on some port I need to tell that
for this to work of course we have set
up a server somewhere we have an HTTP
endpoints they can provide me with the
coordinates that I'm going to draw later
on this is not HTTP communication this
is tcp/ip so I make a connection to that
request to that channel and I can read
whatever comes back so i can do HTTP
communication like this there is you
very few lines of code so how do we use
that well my idea is to just keep
drawing points
and then from time to time re fret re
fetch the data set and keep drawing
again so this is what I'm do
continuously loop through and draw
points then each minute should I fetch
new data if so fetch it and plot the new
points for the next minute this is how
I'm going to realize my use case now we
need to move one step back again and
think of okay what I made before was a
TCP connection not an HTTP connection so
when i send my request HTTP request i
get the entire HTTP response back not
just the body because this is TCP
connection I just happen to speak HTTP /
that connection so these are the bikes
and then my data so i need to keep that
in mind when i pass the data that i
receive here so this is in fact what i
should do instead so loop through and
plot my points do it again and again and
again at some point it's time to fetch
the data if I do so I request the data I
get my response I pass the data and then
which means if I want to continuously
draw the curve over and over again from
time to time I need to stop and do
something else flex my data so the
drawing process would stop while I do
this this is bad time what's marked with
red here this is where the wire protocol
could come in handy again because that
allowed me to chain a couple of Arduino
boards we saw that I could define a
master that could do things and send it
to the slave so here's my idea I want to
let the master request data whenever
needed and whenever needed pass it on to
the slave that should do nothing but
just draw and draw and draw then from
time to time get an interrupt get new
data and then draw and draw again so
this in fact realizes my use case
and this is how my curve ended up
looking and you can see I have some
electrical noise here I have to do this
with a pretty long exposure time with my
camera and apparently there was some
electrical noise that made not very
accurate every time I draw but this is
in fact the result of what I just did
let's see if we can extend it a bit more
though it works we can do more so what
if we could it seems that my mirrors are
fast enough to do things so if I
couldn't plot my curve then take the
beam down to zero and drag it back then
I would indirectly draw the x the x axis
by doing so and we know or we've seen
that it's pretty easy to do with code
whatever is needed to be done here so we
could actually make that even more
advanced we have digital i/o on the
thing too so why not turn off the laser
why we move it and only turn it on when
we draw so loop through the data points
turn off the laser move it down turn it
off draw the axis turn it off move it to
the next point turn it on and then you
can draw again and this is pretty simple
because we have digital i/o the only
thing that it's not simple is to find a
laser way you could actually do it turn
it on and off and especially because I
needed to bring it on the airplane and
the only thing I could find to bring on
an airplane was this little pin here
just because I could I couldn't help I
asked my daughter to write go to on some
grit paper and then take all the
coordinates and type it into an array
for me and you can actually see here it
is pretty accurate you can almost see
the grits in this let me show you
something more everything I've showed so
far was with the Arduino Uno ports it's
called let's another board the Arduino
yawn this one has not one but two
processes one of them running a linux
kernel this is the schematic way of
putting it it means that if you can come
through this bridge which is pretty easy
you can actually from your arduino code
executes linux commands and why is that
interesting well take a look at my gps
program that i had using the brits
library see in set up britt begin i can
get through to the linux part then i can
actually look at i include the process
library up there then i can actually
spawn a process an issue for instance
occur command so rather than downloading
and passing TCP traffic and HTTP myself
I could actually do everything with a
curl command by using this port that is
a bit more advanced which is a very easy
way to upload things to just curl it up
this sport that i have here with an
antenna this is in fact a telephone as a
little connector for a sim card here
this is a telephone it even has jackets
for earplugs and for microphone the way
you use this is what we've seen before
it's serial communication using the
software serial library you just start
communicating with the thing if it has
something to tell you you can read it or
you can just write things to it and
thinks you can write to a telephone now
way back to my age again is AT commands
so if you could just write the the 80
dial command you could actually make
this thing ring or send an SMS that's an
80 command for that 22 just by riding
through the serial connector you can do
that and if you take a look follow that
link you can actually see how you set up
GPRS communication network communication
using that board so what have we seen so
far we've seen input and output digital
analog we've seen serial communication
we've seen a lot of things but
everything that we've seen is in fact a
combination of those building blocks and
with that you can achieve magic and I
really mean magic here's one of the
projects that I'm working with right now
this is an e-ink display I'm trying to
see if that could be an extended screen
to my phone so I have a little bluetooth
extension and the display and don't tell
anyone but this could be the future
and before we sum up here I would just
like to welcome you on States afterwards
to see the thing here when I played with
this back home I used this as my laser
but I wasn't sure that I could get this
on the plane oh well in fact I was
pretty sure if i could get this on the
plane so i decided not to try to so
here's my travel edition which is not
that advanced and with these words I
wish you happy hacking all of you thank
you babe we do have time for a few
questions any burning questions from the
audience comments yep I and thanks for a
great presentation I understood as much
that the digital-to-analog converter was
to increase resolution but I didn't
understand where in the circuit you
added them would you explain that oh yes
see what the Arduino gave me right out
of the box was an output that can
produce a signal like this but not very
fast this serial bus the wire thing runs
at a hundred kilo hertz so on that you
can plug out those pulses very faster
and this is what the board's use did
that answer your question not really
where I like to welcome you on stage yet
to see it but actually because this is
hooked up to let serial bus is actually
just two wires from the serial bus out
to those are little extension things
that I bought and from there it goes to
the loudspeakers yeah yes oh good don't
forget to vote by the way don't forget
to vote hi thanks for a nice
presentation I heard about another
project cult Raspberry Pi which is also
for playing around so what would be the
difference and bit windows to have no
experience with the Raspberry Pi but I
know it's more powerful so it can
probably or it can for sure do things
that you cannot do with the Arduino but
I doubt that it's so easy that you will
let few code lines can do magic that you
can do with the other we know there's
any of you experience with the Raspberry
mean the arduino the entire Arduino
environment is meant for this and
nothing else so everything has been from
from a programming point of view
everything has been optimized for doing
this and nothing else but this and I
think that's a big difference all right
I think that's the rep thank you again
clapping keep it the guy a hand thank
you