0% found this document useful (0 votes)
60 views

Introduccion A La Programacion Con Ardiuno

The document introduces an Arduino starter kit for beginners to learn about microcontrollers. It explains that the kit and accompanying book will guide the user step-by-step through various electronic projects using the Arduino, requiring only basic materials like an internet connection and flat work surface. The goal is to take users from complete beginner level to an intermediate skill set in using Arduinos for microcontroller programming and electronics.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Introduccion A La Programacion Con Ardiuno

The document introduces an Arduino starter kit for beginners to learn about microcontrollers. It explains that the kit and accompanying book will guide the user step-by-step through various electronic projects using the Arduino, requiring only basic materials like an internet connection and flat work surface. The goal is to take users from complete beginner level to an intermediate skill set in using Arduinos for microcontroller programming and electronics.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Introduction

Everything will be explained in clear and easy to follow


steps. The book contains a lot of diagrams and
photographs to make it as easy as possible to check
that you are following along with the project correctly.

What you will need

Firstly, you will need access to the internet to be able to


Thank you for purchasing the Earthshine Electronics download the Arduino IDE (Integrated Development
Arduino Starter Kit. You are now well on your way in Environment) and to also download the Code Samples
your journey into the wonderful world of the Arduino and within this book (if you donʼt want to type them out
microcontroller electronics. yourself) and also any code libraries that may be
necessary to get your project working.
This book will guide you, step by step, through using
the Starter Kit to learn about the Arduino hardware, You will need a well lit table or other flat surface to lay
software and general electronics theory. Through the out your components and this will need to be next to
use of electronic projects we will take you from the level your desktop or laptop PC to enable you to upload the
of complete beginner through to having an intermediate code to the Arduino. Remember that you are working
set of skills in using the Arduino. with electricity (although low voltage DC) and therefore
a metal table or surface will first need to be covered in
The purpose of this book and the kit is to give you a a non-conductive material (e.g. tablecloth, paper, etc.)
gentle introduction to the Arduino, electronics and before laying out your materials.
programming in C and to set you up with the necessary
skills needed to progress beyond the book and the kit Also of some benefit, although not essential, may be a
into the world of the Arduino and microcontroller pair of wire cutters, a pair of long nosed pliers and a
electronics. wire stripper.
The booklet has been written presuming that you have A notepad and pen will also come in handy for drawing
no prior knowledge of electronics, the Arduino out rough schematics, working out concepts and
hardware, software environment or of computer designs, etc.
programming. At no time will we get too deep into
electronics or programming in C. There are many other Finally, the most important thing you will need is
resources available for free that will enable you to learn enthusiasm and a willingness to learn. The Arduino is
a lot more about this subject if you wish to go further. designed as a simple and cheap way to get involved in
The best possible way to learn the Arduino, after using microcontroller electronics and nothing is too hard to
this kit of course, is to join the Arduino Forum on the learn if you are willing to at least ʻgive it a goʼ. The
Arduino website and to check out the code and Earthshine Design Arduino Starter Kit will help you on
hardware examples in the ʻPlaygroundʼ section of the that journey and introduce you to this exciting and
Arduino website too. creative hobby.
We hope you enjoy using the kit and get satisfaction
from creating the projects and seeing your creations
come to life. Mike McRoberts
[email protected]
How to use it May 2009

The book starts off with an introduction to the Arduino,


how to set up the hardware, install the software, etc. We
then explain the Arduino IDE and how to use it before
we dive right into some projects progressing from very
basic stuff through to advanced topics. Each project will
start off with a description of how to set up the hardware
and what code is needed to get it working. We will then
describe separately the code and the hardware and
explain in some detail how it works.
The Starter Kit Contents
Please note that your kit contents may look slightly different to those listed here
What exactly is an Arduino?
speed) and a 5-volt linear regulator. Depending on
what type of Arduino you have, you may also have a
USB connector to enable it to be connected to a PC or
Mac to upload or retrieve data. The board exposes the
microcontrollerʼs I/O (Input/Output) pins to enable you
to connect those pins to other circuits or to sensors, etc.

To program the Arduino (make it do what you want it


to) you also use the Arduino IDE (Integrated
Development Environment), which is a piece of free
software, that enables you to program in the language
Now that you are a proud owner of an Arduino, or an that the Arduino understands. In the case of the Arduino
Arduino clone, it might help if you knew what it was and the language is C. The IDE enables you to write a
what you can do with it. computer program, which is a set of step-by- step
instructions that you then upload to the Arduino. Then
In its simplest form, an Arduino is a tiny computer that your Arduino will carry out those instructions and
you can program to process inputs and outputs going interact with the world outside. In the Arduino world,
to and from the chip. programs are known as ʻSketchesʼ.

The Arduino is what is known as a Physical or


Embedded Computing platform, which means that it is
an interactive system, that through the use of
hardware and software can interact with itʼs
environment.

For example, a simple use of the Arduino would be to


turn a light on for a set period of time, letʼs say 30
seconds, after a button has been pressed (we will build
this very same project later in the book). In this example,
the Arduino would have a lamp connected to it as well
as a button. The Arduino would sit patiently waiting for
the button to be pressed. When you press the button it
would then turn the lamp on and start counting. Once it
had counted 30 seconds it would then turn the lamp off
and then carry on sitting there waiting for another button
press. You could use this set-up to control a lamp in an
under-stairs cupboard for example. You could extend
this example to sense when the cupboard door was
opened and automatically turn the light on, turning it
off after a set period of time.

The Arduino can be used to develop stand-alone


The Arduino hardware and software are both Open
interactive objects or it can be connected to a computer
Source, which means the code, the schematics, design,
to retrieve or send data to the Arduino and then act on
etc. are all open for anyone to take freely and do what
that data (e.g. Send sensor data out to the internet).
they like with it.
The Arduino can be connected to LEDʼs. Dot Matrix
displays, LED displays, buttons, switches, motors,
This means there is nothing stopping anyone from
temperature sensors, pressure sensors, distance
taking the schematics and PCB designs of the Arduino
sensors, webcams, printers, GPS receivers, ethernet
and making their own and selling them. This is perfectly
modules,
legal, and indeed the whole purpose of Open Source,
The Arduino board is made of an an Atmel AVR
and indeed the Freeduino that comes with the
Microprocessor, a crystal or oscillator (basically a crude
Earthshine Design Arduino Starter Kit is a perfect
clock that sends time pulses to the microcontroller to
example of where someone has taken the Arduino PCB
enable it to operate at the correct
design, made their own and are selling it under the
Freeduino name. You could even make your own
Arduino, with just a few cheap components, on a Then, for a couple of quid or bucks you can replace the
breadboard. AVR chip in your Arduino with a new one. The chip must
be pre-programmed with the Arduino Bootloader to
The only stipulation that the Arduino development team enable it to work with the Arduino IDE, but you can
put on outside developers is that the Arduino name can either burn the Bootloader yourself if you purchase an
only be used exclusively by them on their own products AVR Programmer, or you can buy these pre-
and hence the clone boards have names such as programmed from many suppliers around the world. Of
Freeduino, Boarduino, Roboduino, etc. course, Earthshine Design provide pre-
programmed Arduino chips in itʼ store for a very
As the designs are open source, any clone board, such reasonable price.
as the Freeduino, is 100% compatible with the Arduino
and therefore any software, hardware, shields, etc. If you do a search on the Internet by simply typing
will all be 100% compatible with a genuine Arduino. ʻArduinoʼ into the search box of your favourite search
engine, you will be amazed at the huge amount of
websites dedicated to the Arduino. You can find a mind
boggling amount of information on projects made with
the Arduino and if you have a project in mind, will easily
find information that will help you to get your project up
and running easily.

The Arduino is an amazing device and will enable you


to make anything from interactive works of art to robots.
With a little enthusiasm to learn how to program the
Arduino and make it interact with other components a
well as a bit of imagination, you can build anything you
want.

This book and the kit will give you the necessary skills
The Arduino can also be extended with the use of
needed to get started in this exciting and creative
ʻShieldsʼ which are circuit boards containing other
hobby.
devices (e.g. GPS receivers, LCD Displays, Ethernet
connections, etc.) that you can simply slot into the top
So, now you know what an Arduino is and what you can
of your Arduino to get extra functionality. You donʼt
do with it, letʼs open up the starter kit and dive right in.
have to use a shield if you donʼt want to as you can
make the exact same circuitry using a breadboard,
some veroboard or even by making your own PCBʼs.

There are many different variants of the Arduino


available. The most common one is the Diecimila or the
Duemilanove. You can also get Mini, Nano and
Bluetooth Arduinoʼs.
New to the product line is the new Arduino Mega with
increased memory and number of I/O pins.

Probably the most versatile Arduino, and hence the


reason it is the most popular, is the Duemilanove. This
is because it uses a standard 28 pin chip, attached to
an IC Socket. The beauty of this systems is that if you
make something neat with the Arduino and then want
to turn it into something permanent (e.g. Or under-
stairs cupboard light), then instead of using the
relatively expensive Arduino board, you can simply use
the Arduino to develop your device, then pop the chip
out of the board and place it into your own circuit board
in your custom device. You would then have made a
custom embedded device, which is really cool.
Getting Started
This section will presume you have a PC running If you have a Mac these are in the drivers directory.
Windows or a Mac running OSX (10.3.9 or later). If you If you have an older Mac like a PowerBook, iBook, G4
use Linux as your Operating System, then refer to the or G5, you s h ou l d use the PPC dr i vers :
Getting Started instructions on the Arduino website at FTDIUSBSerialDriver_v2_1_9.dmg. If you have a
http://www.arduino.cc/playground/Learning/Linux newer Mac with an Intel chip, you need the Intel dr
i v e r s :
FTDIUSBSerialDriver_v2_2_9_Intel.dmg.
Double-click to mount the disk image and run the
included FTDIUSBSerialDriver.pkg.
The latest version of the drivers can be found on the
FTDI website.

Connect the Freeduino

First, make sure that the little power jumper, between


the power and USB sockets, is set to USB and not
EXTernal power (not applicable if you have a
Roboduino board, which has an Auto Power Select
function).
Get the Freeduino and the USB Cable U s i n g t h i s
jumper you can
Firstly, get your Freeduino board and lay it on the table either power the
in front of you. Take the USB cable board from the
and plug the B plug (the fatter USB port (good
squarer end) into the USB socket for low current
on the Freeduino. devices like
LEDʼs, etc.) or
At this stage do NOT connect the from an external
Freeduino to your PC or Mac yet. power supply (6-12V DC).

Download the Arduino IDE Now, connect the other end of the USB cable into the
USB socket on your PC or Mac. You will now see the
Download the Arduino IDE from the Arduino download small power LED (marked PWR above the RESET
page. As of the time of writing this book, the latest IDE switch) light up to show you have power to the board.
version is 0015. The file is a ZIP file so you will need to
uncompress it. Once the download has finished, unzip If you have a Mac, this stage of the process is complete
the file, making sure that you preserve the folder and you can move on to the next Chapter. If you are
structure as it is and do not make any changes. using Windows, there are a few more steps to complete
(Damn you Bill Gates!).
If you double-click the folder, you will see a few files
and sub-folders inside.

Install the USB Drivers

If you are using Windows you will find the drivers in


the drivers/FTDI USB
Drivers directory of the
Arduino distribution. In the
next stage (“Connect the
Freeduino”), you will point W
i n d o w ʼ s Add New
Hardware wizard to these
drivers.
On Windows the Found New Hardware Wizard will Uncheck “Search removable media”. Check “Include
now open up as Windows will have detected that you this location in the search” and then click the Browse
have connected a new piece of hardware (your button. Browse to the location of the USB drivers and
Freeduino board) to your PC. Tell it NOT to connect to then click Next.
Windows update (Select No, not at this time) and then
click Next.

The wizard will now search for a suitable driver and then
tell you that a “USB Serial Convertor” has been found
On the next page select “Install from a list or specific and that the hardware wizard is now complete. Click
location (Advanced)” and click Next. Finish.

Make sure that “Search for the best driver in these You are now ready to upload your first Sketch.
locations” is checked.
Upload your first Sketch
you will see the Sketch inside the white code window.

Now, before we upload the Sketch, we need to tell the


IDE what kind of Arduino we are using and the details
of our USB port. Go to the file menu and click Tools,
then click on Board. You will be presented with a list of
all of the different kinds of Arduino board that can be
connected to the IDE. Our Freeduino board will either
be fitted with an Atmega328 or an Atmega168 chip so
choose “Arduino Duemilanove w/ATmega328” if you
have a 328 chip or “Arduino Diecimila or Duemilanove
w/ ATmega168” if you have a 168 chip.

Now that your Freeduino has been connected and the Now you need to tell the IDE the details of your USB
drivers for the USB chip have been installed, we are port, so now click on Tools again, scroll down to Serial
now ready to try out the Arduino for the first time and Port and a list of the available serial ports on your
upload your first Sketch. system will be displayed. You need to choose the one
that refers to your USB cable, which is usually listed as
Navigate to your newly unzipped Arduino folder and something like /dev/tty.usbserial-xxxx on a
look for the Arduino IDE icon, which looks something Mac or something like Com 4 on Windows so click on
like this.... that. If not sure, try each one till you find one that works.

Double click the ICON to open up the


IDE. You will then be presented with a
blue and white screen with a default
sketch loaded inside.

This is the Arduino IDE (Integrated Development


Environment) and is where you will write your Sketches
(programs) to upload to your Arduino board. Now that you have selected the correct board and USB
port you are ready to upload the Blink Sketch to the
We will take a look at the IDE in a little more detail in board.
the next chapter. For now, simply click File in the file
menu and scroll down to You can either click the Upload button, which is the 6 th
Sketchbook. Then scroll button from the left at the top with an arrow pointing to
down to Examples and the right (hover your mouse pointer over the buttons to
c l i c k it. You w i l l be see what they are) or by clicking on File in the file menu
presented with a list of and scrolling down to Upload to I/O Board and clicking
Example sketches that you on that.
can use to try out your
Arduino. Now click on Presuming everything has been set up correctly you will
Digital and inside there you now see the RX and TX LEDʼs (and also LED 13) on
will find an example Sketch called Blink. Click on this. the Freeduino flash on and off very quickly as data is
uploaded to the board. You will see Uploading to I/O
The Blink Board.... Just below the code window too.
Sketch will n
owb e
loaded into
the IDE and
Once the data has been uploaded to the board
successfully you will get a Done Uploading message in If so, congratulations, you have just successfully
the IDE and the RX/TX LEDʼs will stop flashing. installed your Arduino, uploaded and ran your first
sketch.
The Arduino will now reset itself and immediately start
to run the Sketch that you have just uploaded. We will now explain a bit more about the Arduino IDE
and how to use it before moving onto the projects that
The Blink sketch is a you can carry out using the hardware supplied with the
very simple sketch kit. For our first project we will carry out this Blink LED
that blinks LED 13, sketch again, but this time using an LED that we will
which is a tiny green physically connect to one of the digital output pins on
LED soldered to the the Arduino. We will also explain the hardware and
b o a r d and a ls o software involved in this simple project. But first, letʼs
connected to Digital take a closer look at the Arduino IDE.
Pin 13 from the
Microcontroller, and
will make it flash on
and off every 1000 milliseconds, or 1 second.
If your sketch has uploaded successfully, you will now
see this LED happily flashing on and off slowly on your
board.
The Arduino IDE

When you open up the Arduino IDE it will look very The Toolbar consists of 7 buttons, underneath the
similar to the image above. If you are using Windows or Toolbar is a tab, or set of tabs, with the filename of the
Linux there will be some slight differences but the IDE code within the tab. There is also one further button on
is pretty much the same no matter what OS you are the far right hand side.
using.
Along the top is the file menu with drop down menus
The IDE is split up into the Toolbar across the top, the headed under File, Edit, Sketch, Tools and Help. The
code or Sketch Window in the centre and the Serial buttons in the Toolbar provide convenient access to the
Output window at the bottom. most commonly used functions within this file menu.
Verify/ Serial
Stop New Open Save Upload
Compile Monitor

The Toolbar buttons are listed above. The functions of each button are as follows :-

Verify/Compile Checks the code for errors

Stop Stops the serial monitor, or un-highlights other buttons

New Creates a new blank Sketch

Open Shows a list of Sketches in your sketchbook

Save Saves the current Sketch

Upload Uploads the current Sketch to the Arduino

Serial Monitor Displays serial data being sent from the Arduino

The Verify/Compile button is used to check that your The Upload to I/O Board button will upload the code
code is correct, before you upload it to your Arduino. within the current sketch window to your Arduino. You
need to make sure that you have the correct board and
The Stop button will stop the Serial Monitor from port selected (in the Tools menu) before uploading. It
operating. It will also un-highlight other selected is essential that you Save your sketch before you
buttons. Whilst the Serial Monitor is operating you may upload it to your board in case a strange error causes
wish to press the Stop button to obtain a ʻsnapshotʼ of your system to hang or the IDE to crash. It is also
the serial data so far to examine it. This is particularly advisable to Verify/Compile the code before you upload
useful if you are sending data out to the Serial Monitor to ensure there are no errors that need to be debugged
quicker than you can read it. first.

The New button will create a completely new and blank The Serial Monitor is a very useful tool, especially for
Sketch read for you to enter code into. The IDE will ask debugging your code. The monitor displays serial data
you to enter a name and a location for your Sketch (try being sent out from your Arduino (USB or Serial board).
to use the default location if possible) and will then give You can also send serial data back to the Arduino using
you a blank Sketch ready to be coded. The tab at the the Serial Monitor. If you click the Serial Monitor button
top of the Sketch will now contain the name you have you will be presented with an image like the one above.
given to your new sketch.
On the left hand side you can select the Baud Rate that
The Open button will present you with a list of Sketches the serial data is to be sent to/from the Arduino. The
stored within your sketchbook as well as a list of Baud Rate is the rate, per second, that state changes
Example sketches you can try out with various or bits (data) are sent to/from the board. The default
peripherals once connected. setting is 9600 baud, which means that if you were to
send a text novel over the serial communications
The Save button will save the code within the sketch line (in this case your USB cable) then 9600 letters, or
window to your sketch file. Once complete you will get symbols, of the novel, would be sent per second.
a ʻDone Saving message at the bottom of the code
window.
To the right of this is a blank text box for you to enter Across the top of the IDE window (or across the top of
text to send back to the Arduino and a Send button to your screen if you are using a Mac) you will see the
send the text within that field. Note that no serial data various menus that you can click on to access more
can be received by the Serial Monitor unless you have menu items.
set up the code inside your sketch to do so. Similarly,
the Arduino will not receive any data sent unless you
have coded it to do so.
The menu bar across the top of the IDE looks like the
Finally, the black area is where your serial data will be image above (and slightly different in Windows and
displayed. In the image above, the Arduino is running Linux). I will explain the menus as they are on a Mac,
the ASCIITable sketch, that can be found in the the details will also apply to the Windows and Linux
Communications examples. This program outputs versions of the IDE.
ASCII characters, from the Arduino via serial (the USB
cable) to the PC where the Serial monitor then displays The first menu is the Arduino
them. menu. Within this is the About
Arduino option, which when
To start the Serial Monitor press the Serial Monitor pressed will show you the
button and to stop it press the Stop button. On a Mac current version number, a list
or in Linux, Arduino board will reset itself (rerun the of the people involved in
code from the beginning) when you click the Serial making this amazing device
Monitor button. and some further information.
Once you are proficient at communicating via serial to Underneath that is the
and from the Arduino you can use other programs such Preferences option. This will
as Processing, Flash, MaxMSP, etc. To bring up the Preferences
communicate between the Arduino and your PC. window where you can change various IDe options,
such as were you default Sketchbook is stored, etc.
We will make use of the Serial Monitor later on in our
projects when we read data from sensors and get the Also, is the Quit option, which will Quit the program.
Arduino to send that data to the Serial Monitor, in
human readable form, for us to see. The next menu is the
File menu. In here you
The Serial Monitor window is also were you will see get access to options to
error messages (in red text) that the IDE will display to create a New sketch,
you when trying to connect to your board, upload code take a look at Sketches s
or verify code. t o r e d i n y o u r
Sketchbook (as well as
Below the Serial Monitor at the bottom left you will see the Example Sketches),
a number. This is the current line that the cursor, within options to Save your
the code window, is at. If you have code in your window Sketch (or Save As if
and you move down the lines of code (using the ↓ key you want to give it a different name). You also have the
on your keyboard) you will see the number increase as option to upload your sketch to the I/O Board (Arduino)
you move down the lines of code. This is useful for as well as the Print options for printing out your code.
finding bugs highlighted by error messages.
Next is the Edit menu. In here you The next menu in the
get options to enable you to Cut, IDE is the Tools menu.
Copy and Paste sections of code. W ithin this are the
Select All of your code as well as options to select the
Find certain words or phrases within Board and Serial Port
the code. Also included are the we are using, as we did
useful Undo and Redo options when setting up the
which come in handy when you Arduino for the first time.
make a mistake. Also we have the Auto
Format function that
Our next menu is the Sketch menu which gives us formats your code to make it look nicer.
access to the Verify/Compile functions and some other
useful functions you The Copy for Forum option will copy the code within the
will use later on. Sketch window, but in a format that when pasted into
These include the the Arduino forum (or most other Forums for that
Import Library option, matter) will show up the same as it is in the IDE, along
which when clicked with syntax colouring, etc.
will bring up a list of
t h e a v a i l a b l e The Archive Sketch option will enable you to compress
libraries, stored your sketch into a ZIP file and asks you were you want
within you r to store it.

libraries folder.
Finally, the Burn Bootloader option can be used to burn
A Library, is a collection of code, that you can include the Arduino Bootloader (piece of code on the chip to
in your sketch, to enhance the functionality of your make it compatible with the Arduino IDE) to the chip.
project. It is a way of preventing you from ʻre-inventing This option can only be used if you have an AVR
the wheelʼ by reusing code already made by someone programmer and have replaced the chip in your Arduino
else for various pieces of common hardware you may or have bought blank chips to use in your own
encounter whilst using the Arduino. embedded project. Unless you plan on burning lots of
chips it is usually cheaper and easier to just buy an
For example, one of the libraries you will find is ATmega chip with the Arduino Bootloader already pre-
Stepper, which is a set of functions you can use within programmed. Many online stores stock pre-
your code to control a Stepper Motor. Somebody else programmed chips and obviously these can be found in
has kindly already created all of the necessary the Earthshine Design store.
functions necessary to control a stepper motor and by
including the Stepper library into our sketch we can use The final menu is the Help menu were you can find help
those functions to control the motor as we wish. By menus for finding out more information about the IDE
storing commonly used code in a library, you can re- or links to the reference pages of the Arduino website
use that code over and over in different projects and and other useful pages.
also hide the complicated parts of the code from the
user. Donʼt worry too much about using the IDE for now as
you will pick up the important concepts and how to use
We will go into greater detail concerning the use of it properly as we work our way through the projects. So,
libraries later on. Finally within the Sketch menu is the on that note, letʼs get on
Show Sketch Menu option, which will open up the folder
were your Sketch is stored. Also, there is the Add File
option which will enable you to add another source file
to your Sketch. This functionality allows you to split
larger sketches into smaller files and then Add them to
the main Sketch.

You might also like