• No results found

Arduino for Kids Young and Old

N/A
N/A
Protected

Academic year: 2021

Share "Arduino for Kids Young and Old"

Copied!
172
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)
(3)

From legal to ISBN

Arduino® for Kids Young and Old ©2014 Daniel W. Milligan

All content is copyright 2014, Daniel Milligan. All rights reserved. No part of this publication or the files that it is comprised of shall be legally produced, reproduced, or transmitted by any form or any means without the express permission of the publisher.

Published by Daniel Milligan 2014, all photo's and images are copyright Milligan Photography unless otherwise noted.

Limit of Liability and Disclaimer of Warranty: The publisher has made every effort in

preparing this book to ensure that the content is accurate however the information provided herein is provided "as is" and without warranty. Daniel Milligan et al makes no representation or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaims any implied warranties of merchantability or fitness for any particular purpose and shall in no event be liable for any loss of profit or any other commercial damage, including but not limited to special, incidental, consequential, or other damages.

Trademarks: This book identifies product names and services known or suspected to be trademarks, registered trademarks, or service marks of their respective holders. They are used throughout this book in an editorial fashion only. While great care has been taken to appropriately identify these items, there is potential that some may have not been identified as such and Daniel Milligan cannot vouch for the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark, registered trademark, or service mark. Daniel Milligan is not associated with any product or vendor mentioned in this book other then being the primary of Milligan Photography.

First edition 2014

ISBN-10: 0985855630

ISBN-13: 978-0-9858556-3-5

Thanks go out to all of my friends and family, and especially my wife Sylvine for all of the support and love through the years.

The resources linked to at the end of this eBook were valid as of the time of the writing of this eBook and include links to various tools helpful to those getting started with open source hardware and hobby programming / engineering.

(4)
(5)
(6)

Getting started with Ardunio®

F

irst I am going to make some assumptions both knowingly and unknowingly. I work with a lot of this technology on a regular basis and may inadvertently leave something out. It's not that I don't feel it's important or needed but more that I might not think it needs explaining or requires additional detail. With that, if you should have any questions please do not hesitate to email me at .

This book is intended for those wanting to start working with open source hardware and

software in order to build their own creations using tools and components readily available. While I will point you to specific links to learn more about various software and/or hardware modules, I am not going to get into too much of programming skills 101 or hardware design 101. My assumption is that you either have some knowledge in this area or are willing to go and learn it as you go. I will try to cover everything in enough detail that you won't have to dig too far to get additional information however it will vary depending on the individual. As the title of the book states, this book is focused on developing with the Arduino® platform. While a number of the examples and overall concepts will be applicable to a number of micro-controllers, I will be focusing on the Arduino® family of modules and specifically those built around the Atmel® AVR family of devices.

The following are some basic terms I will be using that you should be aware of.

(7)

* As I am based in the United States, a typical AC current coming into each house would be in the 120V range while in Europe, the typical value would be in the 240V range. [ ]. While most items purchased operate via 120V's AC, in actuality they operate on a DC voltage and have transformers inside or as part of the power cord to convert from AC to DC.

Having the right tool is always beneficial when working on any kind of project. The same is true when working with both software and hardware. Before you get started you will want to make sure you have the following items:

An experimental design (software/hardware) used to prove a theory or realize an idea

An open source hardware/software solution used for prototyping An add on card i.e. expander board to be used in conjunction with an Arduino®. This may also be called a daughter board.

Direct Current which is what most prototyping boards utilize

Alternating Current which is the standard current coming into a house and delivered to each outlet within the house *

A high level programming language and environment originally designed by Sun® Computer and now owned by Oracle®

A high level programming language designed at Bell Labs[ ]

An Arduino® term for describing the code to be executed on the Arduino® Universal Serial Bus - standard connection for peripherals such as mice, keyboards, card readers, etc.

Light Emitting Diode

A software application which generates executable code from a source file such as a sketch.

A data type found in a sketch which can be assigned a value. Variables can be of different types to represent different data types such as integers, characters, and floating point numbers

A value that can represent both positive and negative numbers A value that can only represent positive numbers

A value passed into a method during program execution A value returned by a method once it has finished processing

A software construct utilized in high level programming languages such as Java™ and C++

The speed at which a communication device operates at Prototype Arduino® Shield DC AC Java™ C 1 Sketch USB LED Compiler Variable Signed value Unsigned value Parameter Return Value Object

Baud, Baud Rate

(8)

a functioning computer (Macintosh® OSX, Windows® based PC, or Linux®)

an available USB host port. The port should be of the type A which will look

something like this:

a clean area to work in and spread out a bit

internet access to look up information

the Java Runtime Environment which can be downloaded:

the Arduino® development environment which can be downloaded:

** an Arduino® kit such as this one from

*** a breadboard for prototyping circuits

*** jumper wires for connecting the various parts of your circuits together and to the Arduino®

A +5v power supply able to deliver 2 Amps of current such as: .

and a comfortable chair

** Electronic components are very susceptible to static electricity. This means that any static electricity that builds up on yourself could damage the component, like the micro-controller, when you touch it and transfer that energy from yourself to the component. In some cases, this damage cannot be detected and the component will work most of the time but not always.

Consider it like a crack in a bowl. It may be fine most of the time however once in a while, some liquid may spill. And then if it happens to be hot, you will get burned.

here

here

MakerSHED

(9)

*** If you happen to purchase the Arduino® kit as specified above then there will be a breadboard and jumpers included with it.

The following list are some additional items which are not required however I regularly use during development.

a soldering iron for building circuits

a solder sucker for removing the excess solder applied with the soldering iron

solid strand wire in the 22 gauge (AWG) area of thickness

wire strippers

wire cutters

small pliers

a multimeter for measuring the voltage at a given point and checking continuity within a circuit.

(10)

Finally a list of some items that you will probably not need however knowing they exist is always good.

a logic analyzer for debugging protocol issues such as those that might come up when using serial peripheral interfaces (SPI) among other things

an oscilloscope for debugging issues related to communication signals or Pulse Width Modulation (PWM) output

(11)

electronics store catering to hobbyists in your corner of the world. In some cases, the devices used will only be available from on-line merchants which I will attempt to provide various links to companies I have dealt with successfully.

All circuit diagrams included in this book are provided using the creative commons license for

Attribution - Share Alike 3.0 Unported[ ]. The complete set of circuit diagrams can be found here:

. Each of the circuit diagrams were created using the Fritzing development tool which can be found on the web at . For each image that is bound by the creative commons license, the following icon will be associated with it: . In addition, I have followed

standard practice in coloring the wires at least in terms of +5v (red) and GND (black). For the

schematics, opposed to using traditional schematics, I have chosen to use the breadboard images out of Fritzing as I feel they better convey the required information in this context.

With all of that said, lets dive into it and have some fun!

3 Circuit Diagrams

(12)
(13)

Getting ready to develop

S

etting up your computer for development can be a challenge depending on the tools and the support available. Hopefully that will not be the case with the Arduino® toolset. However, just in case, I will walk you through the process. If you are already up and going then please skip over this chapter and proceed to developing your first sketch in the chapter.

While you may already have Java™ installed on your computer, it wouldn't hurt to ensure you have the latest version so please download the following package from Oracle® . This page should detect your current system and give you a download button to start. Once downloaded, follow the directions provided by Oracle® to install this on your system.

Now that Java™ is ready to go, you can download the Arduino® environment for development. This software can be found . For this package, there isn't an installer* per se however you do need to unzip it. Navigate to your download folder or the destination that you selected when

downloading the package to get access to the application. If you are on a Macintosh OSX® platform than it will unzip the package for you. If you are on a Windows® platform than the Explorer

application will be able to unzip it for you. For the Linux® platform, when you download it, I expect it will ask you if it should open it with the archive manager which you can choose to or from the terminal you can utilize the unzip application. For my own development, I moved the package to my

Development folder which is located on my desktop. I than unzipped it in that location so it will be

centrally located with other things I am working on.

* As of the 1.05 version of the Arduino™ software for Windows® there is an installer available to assist with installing the Arduino™ software. I have left this chapter in for reference and for those not on a Windows® based platform.

The following sequence is the platform extraction and placement on my Windows® based machine and the initial loading of the platform. The first image shows the file once it has been downloaded from the Arduino™ website.

Arduino® Platform

here

(14)

This image shows the Windows® wizard for extracting a zipped file. I have selected my

Development folder on my desktop for the placement of the platform.

This image shows the Windows® extraction in progress which on my system is fairly slow as it is not the fastest system in the world.

(15)

This image shows the extracted file folder which has inside it the actual Arduino® folder. Because I didn't want to have to go too deep into the folder hierarchy, I elected to move it up one.

(16)

This image shows the extracted folder once I moved it up one level, removing the arduino-1.0.3-windows folder.

In this image, I have elected to display everything as large icons. At this point you will be ready to launch the development platform.

When you first launch the application, you will most likely see the following security warning. This is expected and is just noting to you, the end user, that the file was downloaded and the publisher is unknown. While I don't expect you to have any issues running this software, I do advise that you take the necessary precautions with your system and ensure that you have up to date virus protection installed and running. Also note the checkbox which will allow you to skip the security warning the next time you open the application.

(17)
(18)
(19)
(20)
(21)

What it is

T

he Arduino® package is a cost effective, easy to use, prototyping board which can be enhanced to do many tasks. The micro-controller that this board utilizes has been created with a number of general purpose input and output (GPIO) pins. These GPIO pins have many functions that you can utilize to create a number of designs. While the controller and overall design are fairly simple in todays terms, the community that has developed around the platform is where it really shines. With a simple internet search you can find a number of forums and stores promoting and selling various products to enhance your Arduino®, from Ethernet port shields to LCD shields to a number of other's in between.[ ] Considering all of these expansion options are readily available, the Arduino® becomes a very useful micro-controller. In this book I will focus on the abilities of one of the Arduino® boards, the Arduino® UNO. It does everything that we need and is based on a 5 volt system which can be used with a variety of external devices. This will also limit the discussion to one of the more popular Atmel® AVR micro-controllers, the ATmega328.

(22)

What it isn't

T

his is not a full fledged computer with monitor, keyboard, and mouse as you have probably already noticed. While it is fast doing simple tasks, it will not break any super computer processing speeds. With today's processors running at 100x or 200x the speed of this little controller, it isn't surprising that it isn't as fast as a full fledged computer. The overall Arduino® environment is based on approachable and easy to use tools. The environment does it's best to prevent mishaps when using it and the target devices. With this extra security comes limitations and potential speed losses. There is also potential for conflicting devices due to the overloading of processor pins to internal functions. However for the kinds of projects that we will want to do, it is more than capable.

Getting Started

So where do we start? Assuming you have already purchased an Arduino® and have the

development environment installed, our first order of business is to make sure everything is working ok. If you haven't already downloaded and configured your system, take a look at the chapter entitled for information regarding setting up your development environment. When ready, open up the development platform and you should see something like this:

(23)

Now we need to make sure your Arduino® is properly registered with your development environment. If you have not connected your Arduino® to your development system than you may need to load the drivers in order for the board to communicate with your system. On the Windows™ platform, you can find the necessary drivers in the drivers folder under the Arduino® software folder. If you are running on the Macintosh® or Linux® then you will want to visit the USB chip

manufacturer web page to download the drivers for your particular system. They can be found . You will also find the latest Windows® drivers there.

The first thing to notice in the previous image is the currently selected board and

(24)

communication port i.e. COM port. This is shown in the lower right hand side of the image. This development board is an Arduino® UNO connected to COM3. In order to work with your Arduino® you will need to ensure the correct COM port is selected along with the correct hardware board. To do this, select the tools menu item on the menu bar.

Note: the menu can take some time to actually pop up depending upon the user's environment.

From the tools menu, select board and then select the type of board that you have. The

(25)

Once the proper board has been selected, the corresponding COM port that the operating system has assigned to the development board can be selected. The following image shows the selection of the COM port for the attached Arduino® board.

Before we go any further, it will be good to look at some of the different variable types that you may find within an Arduino® sketch. The following are some of the more common variable types you may come across:

(26)

* the size of an int or long may be different based on the processor in use. For the Atmel® micro-controller we are using an integer will be 16 bits i.e. 2 bytes while the long will be 32 bits i.e. 4 bytes. Other micro-controller's may have larger integer sizes. You should verify the sizes of the data types of your target hardware to ensure you do not make any false assumptions.

** floats are stored as 32 bits of data which represents the mantissa and the exponent. Floats have about 7 decimal digits of precision which should be considered when processing data.

Every Arduino® sketch will consist of two functions i.e. methods, setup and loop. As you can probably imagine, setup is where you will configure your Arduino® for operation such as specifying the direction of data flow on a certain pin etc. The loop function is where all of the processing is done and will be executed basically as fast as the micro-controller can go. Let's take a look at each of these methods in some detail and see how they work.

Setup

As mentioned above, this is where the configuration of the micro-controller is performed. This isn't to say that the micro-controller configuration cannot be changed outside of this method however for consistency it is best to do any configuration here. There will be exceptions to this rule but for now this will be good advice to follow. So what kind of things should be configured when you start executing code on your Arduino®? Primarily it will be proper to configure your digital pins as either

a signed value representing the values from -128 to 127. Char's are stored using 8 bits of data

an unsigned value representing the values from 0 to 255. Like a char, an unsigned char is stored using 8 bits of data

Analogous to an unsigned char and used within Arduino® sketches

a signed integer type representing the values from -32,768 to 32,767. Int's are stored using 16 bits of data*

an unsigned integer type representing the values from 0 to 65,535. Int's are stored using 16 bits of data*

a signed value representing the values from -2,147,483,648 to 2,147,483,647. Long's are stored using 32 bits of data.*

an unsigned value representing the values from 0 to 4,294,967,295. Unsigned long's are stored using 32 bits of data.*

a float value represents a wide range of signed values in decimal notation and is primarily used to represent fractions and other non integer values. Float's are stored as 32 bits of data.**

For the Arduino® platform, the double is analogous to a float.

the void type typically indicates a non-value however is dependent on its usage. For method definitions, a return value of void indicates that no value will be returned by the method.

char unsigned char byte int unsigned int long unsigned long float double void

(27)

input or output and start any sub-systems that may need to be started such as the serial

communications port. The main reason is that this method is only called once while the loop is called

every pass through the code. It wouldn't be very productive to keep initializing the serial

communications and the like every time through the main loop. Consider the following code snippet from this :

/*

* Setup - main configuration point of our sketch to configure * the Arduino for operation

* * Params - none * * Returns - nothing */ void setup() { Serial.begin(9600); pinMode(13, OUTPUT); }

The first portion are the comments describing what the intention of the method is. I say

intention as with all things, they start as good intentions and then veer off from there. For Arduino®

sketches, there are two acceptable types of comments, the block as shown above and the single line [ ]. The block comment is everything between the opening token /* and the closing token */ which indicates to the Arduino® compiler that what lies between the tokens including the tokens themselves can be ignored. While the above example shows a block comment spanning multiple lines, there is nothing preventing it from being a set of single line comments such as:

//

// Setup - main configuration point of our sketch to configure // the Arduino for operation

// // Params - none // // Returns - nothing //

I would be reluctant to put the whole thing on one line as I prefer to have some whitespace i.e. blank area around the comments for readability. You could do something like the following:

// Setup - main configuration point of our sketch to configure the Arduino

sketch

(28)

for operation

The other things of note inside of the comment block is the Params and Returns lines. The first tells the viewer of your code what parameters need to be passed into the method. As shown above, not all methods require parameters. The second line indicates what values one might expect to be returned from the method when it finishes. In this case, nothing is returned. Later on we will explore methods that take both parameters and return values to the calling application. This convention is something I tend to use most often and part of the reason why I don't use a single line comment at the beginning of functions i.e. methods. Even though nothing is passed in as a parameter nor returned, have it spelled out never hurts.

Following the comment block is the name of the method which declares what the return value type will be, if any, and also details any parameters that need to be supplied when the application calls the method. In this case, the method does not take any parameters and does not return any value. This is indicated by the word void. Next comes the opening brace, { which indicates the start of the method definition. There is also a closing brace, } which indicates the end of the method definition. The opening and closing braces are not limited to method definitions and will be used in additional constructs which will be explored later on. Now we get to the actual code. The first line is as

follows:

Serial.begin(9600);

This code, which is part of the standard Arduino® platform, calls the begin method which is part of the Serial object. An object is a collection of related methods and variables that work together to perform a task. The C++ term for an object is a class which is fundamental to C++ and object

oriented programming in general. We will be looking at various objects and constructs as we

progress through this book. As you can see, the begin method takes a single parameter which in this case is a value of 9600. This value represents the speed at which the serial port of the Arduino® will operate at. While the speed can be a number of values [ ], 9600 is one that I remember easily and typically use. In terms of speed, this value is typically given as a baud rate which is the number of symbols transmitted per second. This value happens to be the same as the bit rate resulting in 9600 bits per second in transmission speed. The end data rate however will be somewhat less due to some of the bits being used to indicate the start and ending of each packet. The main thing to note is that when you open up a serial monitor application such as the one included with the Arduino® software, you will need to ensure that it is set for the same speed that was specified in the sketch. So why would we want to start the serial port? In general this is the simplest way to debug your application. You upload your code, it executes and you monitor the results. It can be real useful when things are not working as expected and you need to dig in and figure out what the issue is. If application speed is a concern, than once you have completed your project, you can remove all of this code resulting in a smaller package and faster execution time. A few things to be careful of though is that for time

sensitive applications, the inclusion or exclusion of this code could change your results. Also care should be taken when placing any types of debug lines as you may end up with more information than

(29)

you can use.

pinMode(13, OUTPUT);

The second line of code, also a standard part of the Arduino® platform, is where the digital pin, 13, is configured as an output. On the Arduino®, there is a LED connected to the digital pin number 13 which can be used as a visual indicator of sorts. The first parameter is the pin that will be configured while the second parameter is the mode which in this case is OUTPUT. The word

OUTPUT is a constant value defined by the Arduino® platform. Typically it is good practice to

create a constant value to specify what the particular pin will be doing opposed to a raw number. In this case, I left the number in the code to highlight what pin I was using and to keep the sketch simple. In the future examples, I will use more meaningful identifiers for these types of values.

Loop

Loop is where all of the main processing of your sketch will be done. This method will be

executed soon after the Setup method has completed and will continue to be executed for the life of your application that is to say until you power down your Arduino®. While this keeps the complexity of your code down, there are a few things to be careful of when writing code for the loop. The first thing to remember is that the speed at which loop is called can be variable based on the speed of the micro-controller and how much work is being done inside of your loop. Another thing to note is that if you need to receive input from the user, you may need to do that over multiple passes of the loop

opposed to waiting until the input arrives otherwise you will prevent other activities from happening. Building upon the above code, I will demonstrate the basic sketch of turning the pin 13 LED on and off periodically causing it to flash. This is also included in the sketch noted previously.

/*

* loop - main method which will be called each time the * Arduino goes through its main loop

* * Params - none * * Returns - nothing */ void loop() { digitalWrite(13, HIGH); delay(500); digitalWrite(13, LOW); delay(500);

Serial.println("Led 13 has been toggled on and off."); }

(30)

Again the method starts with a comment block describing what the method is intended to do along with the name of the method declaration indicating that no parameters are passed to the method and the method doesn't return any value.

digitalWrite(13, HIGH);

This first line of code calls the method digitalWrite which instructs the micro-controller to set the output value on pin 13 to HIGH. The word HIGH is another predefined value which you can use throughout your sketch as needed. Because we are writing a digital value out to this pin, it can only be one of two values, LOW or HIGH. These values correspond to 0 volts when set to LOW and 5* volts when set to HIGH. With this, the LED connected to pin 13 will turn on as there is now a positive voltage emitting from the given pin. When set to LOW, the voltage is set to 0 volts resulting in the LED turning off.

* the Arduino® UNO runs on 5 volts hence the value when set to HIGH will be 5 volts. Other boards, such as the , run at a lower voltage such as 3.3v so the output voltage when set to HIGH will be equivalent to that lower voltage.

When using a board with a lower overall voltage, care must be taken that the resistors and other components recommended in this book are adjusted

accordingly. Because of the smaller voltage, less current will be generated as the current is equal to the voltage divided by the resistance. While this may be

alright in most cases, you should also verify the maximum amount of current that each output pin can supply as they may differ across micro-controller devices.

delay(500);

The delay method is used to stop execution for a period of time. When I say stop execution, I really mean stopping execution of your code. The micro-controller is still processing data however your code will be paused until the delay method is finished. For this example, using delay is fine however later on we will explore better approaches to handle this. So why would I want a delay in my code anyways? Remember that the loop method is executed quite frequently and we want to be able to see the LED turn on an off. The delay that is specified is 500. This value is expressed in milliseconds which is to say how many 1/1000's (0.001) of a second the micro-controller should pause. For the above example, we are pausing for 500 milliseconds which is 1/2 second. The end result is the LED will turn on for 1/2 second and then back off for another 1/2 second or so. I say so because I have an additional line of code after the second delay which will take some time to execute and will skew the time. I could try and compensate by reducing the second delay to 499 milliseconds however that would be a guess unless I knew for sure how long the last line of code would take to execute. I would also have to factor in which micro-controller was executing the code along with the speed of that micro-controller. All in all it would be a significant challenge to do correctly. And

(31)

while were are speaking of the last line of code, it's purpose is to just give us feedback that the loop is working properly and our board is functional. Typically I wouldn't do this as it will just fill up the communications buffer and if I didn't have the delay(s), we would quickly fill our monitor buffer with the same not so helpful information. This is an example of where you would want to be careful with where you place your debugging i.e. informational output data.

Serial.println("Led 13 has been toggled on and off.");

The serial class has a number of methods of which one of them is shown in the above code snippet. This particular version is for printing out a full line of text including the line feed so the next line will start on a new line. There is a related method Serial.print that will print out text much the same way however will not include a line feed. It is useful when you want to print out a number of items all on one line such as:

Serial.print("Led: "); Serial.print(13);

Serial.println(" has been toggled on and off.");

The above code will result in the same output however I could have easily replaced the number

13 with a variable in order to use this code for any pin.

Summary

So pulling it all together, an Arduino® sketch consists of two methods, the setup and loop methods. The setup method is used to configure your Arduino® for how it should operate while the

loop method is where all of the work will be done for the life of the application. In addition, there are

built in constants and classes such as the Serial class which can be used to assist with debugging or relaying information to the user.

(32)
(33)
(34)

Monitoring our surroundings

N

ow that we have the Ardunio® setup and ready, we can dig into more interesting topics. The first of these is the input system that allows us to monitor our surroundings utilizing various analog and digital devices. In the following sections we will look at analog and digital inputs from a variety of devices which allow us to interpret our surroundings and act upon the various events when they happen.

Analog Input

The Arduino® has several analog pins that can be used to read in the voltage level of a

particular device to determine its value. The actual number of pins available to you will be dependent upon which AVR micro-controller is built into the board you have purchased. Regardless though, the basic operation will be the same. The analog input pins have a 10 bit resolution which is to say that each pin can detect up to 1024 levels of voltage. The values are based on ground as being the bottom i.e. no voltage coming in and up to five (5)* volts which is the maximum voltage for the

micro-controller. You can also limit the top of the voltage range to a value less then 5 volts however for the following examples that will not be necessary. When the analog input is read in, it is actually using a scale much like a balance scale to determine the value. Imagine you have a basement that is flooding and there is a staircase coming up to the first floor. One thing you might want to know is how deep the water is and how fast is it rising? For the depth of the water it would be very easy if you happened to know how far apart each step on the staircase is. In this case, you could simply count how many steps were covered in water and do some simple arithmetic to determine the waters depth. Oddly enough the Arduino® is doing something very similar. It has a built in scale that compares its internal value with the value coming in on the analog pin. Each cycle, the device will increase the voltage and compare it to the incoming voltage. It knows how many cycles it has been since it started the

comparison so once they are equal, the micro-controller knows the value of the external voltage. The micro-controller has 10 bits of resolution which means that it can count all the way from zero (0) to 1023 to find a value matching that which is being read at the analog input pin. Using the above analogy, it is like the micro-controller has a staircase with 1024 steps counting the floor as step number one (1). In the case, step number one is equal to zero (0) volts and step number 1024 is equal to five (5) volts. This corresponds to a voltage of 0 volts being equivalent to a digital value of 0 and a voltage of 5 volts being equivalent to a digital value of 1023. As you can see, there is a fair amount of precision when using the 10 bit analog to digital converter. The downside to this type of converter is that it takes time to count from zero (0) up to the value that represents the incoming analog voltage. Just like the rate that the water rises in the basement, the comparison of each voltage value to the next highest value takes time as the micro-controller determines the equivalent voltage to the input voltage. The worst case of course is for 5 volts which is at the top of the range resulting in the

micro-controller having to count 1024 times to get to a comparative value. In terms of computing time, this can be quite slow and could be as much as 260 microseconds for the given device. In human time, this

(35)

is about 1/4 of a millisecond which in turn is 1/1000's (0.001) of one second. When looked at in this way, it is plenty fast enough for what we want to do. Speaking of what we want to do, the first thing is to determine what the temperature in the room is.

* For the Arduino UNO and MEGA, the maximum voltage that can be read in is 5 volts however on some systems, the maximum may be lower such as 3.3 volts or even 1.8 volts.

TEMPERATURE READING

The first thing we will look at is how to read in a temperature value and then be able to react to it based on its value and/or delta from it's initial value. Luckily there are specific devices available that will increase in voltage as the surrounding temperature rises. Knowing this, we can connect one of these devices to our Arduino® and read in the value to determine the current temperature.

REQUIRED COMPONENTS

Arduino® Board 4.7K Ohm Resistor

LM335 temperature sensor[ ] or similar such as the NTE7225[ ]

TOOLS

breadboard jumper wires voltage meter

SCHEMATIC

For this circuit, you will need to run 5 volts from the Arduino® to the first lead of the 4.7K

Ohm resistor. The second lead of the 4.7K Ohm resistor is connected to pin two (2) of the LM335. The third pin of the LM335 is connected to ground from the Arduino®. The final wire is connected from pin two (2) of the LM335 back to analog input zero (0) of the Arduino® in order to read the voltage drop across the LM335 as it pertains to the current temperature.

(36)

Image created using:

SOFTWARE

The following is the code that is necessary to read in the analog voltage across the LM335 and display it to the serial port for review. Once we have our circuit working and the temperature values available, we will be able to further enhance the software and hardware to perform some useful functions.

For this sketch, I am using the well known formulas to convert from Celsius to Fahrenheit and from Fahrenheit to Celsius. These formulas are as follows:

Celsius = ((Fahrenheit - 32) x 5) / 9 Fahrenheit = ((Celsius x 9) / 5) + 32

/*

* The temperature pin is where the input from the LM335 will be read from. * The base voltage is what the device (LM335) will measure at 0°C

* The voltage reference is the max voltage that can be read in on the * analog input pin

* The max sample count is the highest value that could be read in on the * analog input pin which will equal the voltage reference

* The c to f conversion is the well known formula of multiplying the * C temp by 9 then dividing by 5

* The f to c conversion is the well known formula of multiplying the * F temp by 5 then dividing by 9

* The c to f delta is the difference between C and F at the freezing temperature

*/

const int temperaturePin = 0;

const float baseVoltage = 2.730f; /* 2.730V at 0°C */ const float voltageReference = 5.0f;

const float maxSampleCount = 1023.0f; const float CtoFConversion = 9.0f / 5.0f; const float FtoCConversion = 5.0f / 9.0f;

(37)

const float CtoFDelta = 32.0f; /* * Method declarations */

float getTemperature(float temperatureVoltage);

float convertTemperatureToF(float temperatureInCelsius); float convertTemperatureToC(float temperatureInFahrenheit);

/*

* getTemperature - determines the temperature in celsius based on the * incoming voltage value assuming 2.73v equals 0°C

*

* Params - temperatureVoltage - the incoming voltage representing the temperature

*

* Returns - float - the actual temperature in celsius based on the incoming voltage

*/

float getTemperature(float temperatureVoltage) {

float currentTemperature = 0.0f;

// From the datasheet, every 10 mv is equal to 1 °K - 0°C is 273°K equal to 2.73v

// Temperature is scaled by 100 to bring it into the celsius scale from kelvin

currentTemperature = (temperatureVoltage - baseVoltage) * 100;

return currentTemperature; }

/*

* convertTemperatureToF - converts the temperature from Celsius to Fahrenheit

*

* Params - temperatureInCelsius - the incoming temperature in Celsius *

* Returns - float - the temperature expressed in Fahrenheit */

float convertTemperatureToF(float temperatureInCelsius) {

float convertedTemperature = 0.0f;

convertedTemperature = (temperatureInCelsius * CtoFConversion) + CtoFDelta; return convertedTemperature; } /*

* convertTemperatureToC - converts the temperature from Fahrenheit to Celsius

(38)

*

* Params - temperatureInCelsius - the incoming temperature in Fahrenheit *

* Returns - float - the temperature expressed in Celsius */

float convertTemperatureToC(float temperatureInFahrenheit) {

float convertedTemperature = 0.0f;

convertedTemperature = (temperatureInFahrenheit - CtoFDelta) * FtoCConversion; return convertedTemperature; } /*

* Setup - main configuration point of our sketch to configure * the Arduino for analog input

* * Params - none * * Returns - nothing */ void setup() { analogReference(DEFAULT); Serial.begin(9600); } /*

* loop - main method which will be called each time the * Arduino goes through its main loop to read the * current temperature value

* * Params - none * * Returns - nothing */ void loop() { int temperatureValue = 0; float currentTempVoltage = 0.0f; float currentTemperature = 0.0f;

// Read in the temperature value which will be in a range from 0 to 1023 temperatureValue = analogRead(temperaturePin);

// Convert our incoming voltage representation to a value we can utilize currentTempVoltage = ((float)temperatureValue * voltageReference) / maxSampleCount;

// We now have the current temperature expressed in celsius currentTemperature = getTemperature(currentTempVoltage);

(39)

// Print out the current temperature in both C and F Serial.print("The current temperature is: ");

Serial.print(currentTemperature); Serial.print("°C which is ");

Serial.print(convertTemperatureToF(currentTemperature)); Serial.println("°F");

// Rest for a second and check again delay(1000);

}

CONSTANTS

In the beginning of this sketch I have declared some constant values which I will use later on in my code. As I mentioned previously, it is better to declare these values as constant that way you get the benefit of meaningful names in your code which helps in the overall readability of your sketch along with the ability to change the value as needed later on without trying to find everywhere it is used in your code. It should be noted that you could also declare these values as defines which act as constants and are replaced in your code by the when you compile the code.

const int temperaturePin = 0;

const float baseVoltage = 2.730f; /* 2.730V at 0°C */ const float voltageReference = 5.0f;

const float maxSampleCount = 1023.0f; const float CtoFConversion = 9.0f / 5.0f; const float FtoCConversion = 5.0f / 9.0f; const float CtoFDelta = 32.0f;

#define temperaturePin 0 #define baseVoltage 2.730f /* 2.730V at 0°C */ #define voltageReference 5.0f #define maxSampleCount 1023.0f #define CtoFConversion 9.0f / 5.0f #define FtoCConversion 5.0f / 9.0f #define CtoFDelta 32.0f

In the above code snippet, I have declared each variable as const indicating that the value will remain constant for the life of the application. Basically I am telling the compiler that I want to be able to read the value stored in the variable however I never want to change it during the lifetime of my application by writing to it. Given the type of values I am using, there is no reason to change them while the application is being executed. My main goal with these variables is to have human readable names assigned to the values so that my application is easier to understand. As shown in the second section, I could have also declared these constants using the #define pre-processor directive which would also make my code readable and maintainable. In doing so, however, I lose the type

information of each of the values which may or may not be of interest to you the developer. On the flip side, the use of #define could reduce the amount of RAM that is used for your sketch.

(40)

Another thing you may have noticed is that for every variable of type float, I added an f to the end of the declaration. This is called a suffix which I tend to use on any value I assign to a float. By default, the compiler will consider a decimal value of type double and therefore convert the value from double to float when it builds the application from the sketch. For the Arduino® this isn't really a problem given float's and double's are the same thing. On systems where the double is indeed

different, the compiler may generate a compilation error as it will appear that you are trying to assign a double value to a float variable. That would be the best case as you will at least get a chance to look at it and determine if there is a mistake in your code or not. Some compilers may automatically convert the value to float for you resulting in the possibility of a loss of precision.

METHOD DECLARATIONS

The next portion of my sketch consists of the method declarations or function prototypes as they would be named in the C language. These declarations inform the compiler of what methods will be available for use along with what types of variables are passed into the method when it is called along with the type of value that is returned if any. While technically you do not have to give the variables i.e. parameters names in the method declaration, it is good practice to do so in order for someone looking at the code to better understand the values being passed into the method. The only exception to this is the setup and loop methods which are declared for you outside of your sketch.

float getTemperature(float temperatureVoltage);

float convertTemperatureToF(float temperatureInCelsius); float convertTemperatureToC(float temperatureInFahrenheit);

In this sketch, I have declared three methods that I may want to use. In the end I am only going to use two of them however I declared all three for completeness. Because I am going to be looking at values that change in the order of micro-volts, I will need a data type that can represent a decimal value such as a float. The first will convert the raw voltage reading into a temperature in celsius while the other two will convert temperatures between Fahrenheit and Celsius.

USER METHODS

The methods are declared above now appear in the code and this is where I will define what each method actually does. My end goal is that this code has enough comments to make it clear as to what its intended behavior is.

/*

* getTemperature - determines the temperature in celsius based on the * incoming voltage value assuming 2.73v equals 0°C

*

(41)

temperature *

* Returns - float - the actual temperature in celsius based on the incoming voltage

*/

float getTemperature(float temperatureVoltage) {

float currentTemperature = 0.0f;

// From the datasheet, every 10 mv is equal to 1 °K - 0°C is 273°K equal to 2.73v

// Temperature is scaled by 100 to bring it into the celsius scale from kelvin

currentTemperature = (temperatureVoltage - baseVoltage) * 100;

return currentTemperature; }

In the above code, I have a method, getTemperature which takes a temperature reading in volts and converts it to a temperature value in degrees celsius. I could just convert the value read in

directly to Fahrenheit but not everyone is familiar with the Fahrenheit scale and Celsius is closer to the original value making debugging easier if the values look odd from the expected values. The raw temperature value is the value I will be reading from the analog input pin of the Arduino®. While I could keep it as a voltage reading, it will be easier to for me to relate to the value if it is in unit of measure that I am familiar with. In this case having it in a temperature unit is much easier then a raw voltage value.

/*

* convertTemperatureToF - converts the temperature from Celsius to Fahrenheit

*

* Params - temperatureInCelsius - the incoming temperature in Celsius *

* Returns - float - the temperature expressed in Fahrenheit */

float convertTemperatureToF(float temperatureInCelsius) {

float convertedTemperature = 0.0f;

convertedTemperature = (temperatureInCelsius * CtoFConversion) + CtoFDelta;

return convertedTemperature; }

(42)

/*

* convertTemperatureToC - converts the temperature from Fahrenheit to Celsius

*

* Params - temperatureInCelsius - the incoming temperature in Fahrenheit *

* Returns - float - the temperature expressed in Celsius */

float convertTemperatureToC(float temperatureInFahrenheit) {

float convertedTemperature = 0.0f;

convertedTemperature = (temperatureInFahrenheit - CtoFDelta) * FtoCConversion;

return convertedTemperature; }

This method converts a temperature in Fahrenheit to Celsius. SETUP

Again, the setup method is where I will configure my Arduino® to operate as I need. In this case I will want to set the analog reference voltage to the default which in the case of the Arduino® UNO or the Arduino® MEGA, will be 5.0v. This informs the micro-controller I want to measure voltages in the range of 0v to 5.0v. For our purposes, this resolution will be fine.

void setup() { analogReference(DEFAULT); Serial.begin(9600); }

So what do I mean regarding resolution? Well the analog to digital converter in the Atmel® micro-controller has 10 bits of data for precision. For a 5.0v voltage range, this means that there will be 5.0v / 1024 or 0.0048828125 volts per step of the analog to digital converter. For each increment of the analog to digital converter, the voltage that it represents increases by 0.0048828125 volts. I have also configured my serial port here so I can write sample data out to my development system to verify that the data looks appropriate.

(43)

The loop method once again is where all of the real work is done. Here I am also using

comments in the code to explain what everything is doing so the next person looking at this code can understand it without any assistance from me.

void loop() { int temperatureValue = 0; float currentTempVoltage = 0.0f; float currentTemperature = 0.0f;

// Read in the temperature value which will be in a range from 0 to 1023 temperatureValue = analogRead(temperaturePin);

// Convert our incoming voltage representation to a value we can utilize currentTempVoltage = ((float)temperatureValue * voltageReference) / maxSampleCount;

// We now have the current temperature expressed in celsius currentTemperature = getTemperature(currentTempVoltage);

// Print out the current temperature in both C and F Serial.print("The current temperature is: ");

Serial.print(currentTemperature); Serial.print("°C which is ");

Serial.print(convertTemperatureToF(currentTemperature)); Serial.println("°F");

// Rest for a second and check again delay(1000);

}

The first thing that is done is to read in the current temperature value in volts as a digital

representation. Once a value has been read, it can then be converted for display to the end user. In the above code, I first convert the temperature from a digital representation to an actual temperature. Given the LM335 is designed to be sensitive to 1°Kelvin which represents 10 milli-volts (mV) for each one degree Kelvin of change, I will convert from the digital representation to a Celsius

temperature value. So how did I go from Kelvin to Celsius? Kelvin has the same scale as Celsius however it starts at absolute zero and continues up on the same scale. With absolute zero being

-273.0°C, I simply use this value to convert from Kelvin to Celsius. Once I have done that, I can then use one of my methods I declared above to convert from the Celsius value to Fahrenheit. Once I have my values in the units of measure that I want, I can then print them to the serial port for verification on my development system.

A sample of the output generated by this sketch is as follows:

The current temperature is: 26.61C which is 79.90F The current temperature is: 26.12C which is 79.02F The current temperature is: 26.12C which is 79.02F The current temperature is: 26.12C which is 79.02F

(44)

The current temperature is: 28.08C which is 82.54F The current temperature is: 29.05C which is 84.30F The current temperature is: 29.54C which is 85.17F The current temperature is: 28.56C which is 83.42F The current temperature is: 28.08C which is 82.54F The current temperature is: 27.59C which is 81.66F

As you can see in the above data, my temperature readings are in the expected range i.e. room temperature or close to it depending on the room etc. Also you should note that I placed my finger on the device during the output display which is why the temperature started to rise and then began to fall once I removed my finger from the device. I did this to ensure that the circuit and code were working as expected. The only thing I have not done is to calibrate my temperature device to ensure it is

reading the correct value. This is done by connecting the ADJ pin of the temperature sensor to a potentiometer and than fine tuning the input to the current room temperature.

Image created using:

In the above circuit diagram you can see where I have placed a potentiometer attached to the

ADJ input of the temperature sensor to allow me to calibrate it to the current room temperature. While

not overly critical for what we are doing, having the ability to do this will be crucial when we want to use the same basic circuit to monitor temperatures with a suitable level of accuracy.

For analog inputs, that covers it for the most part. There are more fine details that could be discussed such as over voltage prevention, static electricity, etc. however as an introduction, I think we have the basics covered. One thing you may have noticed is that under tools, I mentioned a voltage meter however did not mention when it should be used. The voltage meter is one of those tools that I have handy whenever something doesn't look right. For sanity sake, I can use the voltage meter to ensure that I have wired my LM335 correctly and it is measuring a voltage in the general range I expect. Typical voltage meters are actually multimeters which can measure in addition to voltage, current and resistance. One such feature on my multimeter is the continuity check where I can ensure that I have wired things properly by checking two points and audibly verifying that I have continuity between them. So while I may mention that a voltage meter or multimeter is one of the tools

recommended, I may not indicate when I use it. Basically it is one of those tools that you should have for whatever circumstances come up.

(45)

Digital Input

For digital inputs, the Arduino® has a number of pins available to accomplish this. When we say digital input, we are referring to the micro-controller being able to recognize an incoming signal as either HIGH or LOW. For the devices we are looking at, this would be equivalent to zero (0) volts representing a LOW value and five (5) volts representing a HIGH* value. The main thing to

remember with digital inputs is that the value to trigger either a HIGH or LOW is not exact. There is a range of values that will cause the input to be HIGH or LOW. The datasheet indicates that a LOW signal is anything from -0.5v through 1.5v when the micro-controller is powered by 5v. The datasheet also indicates that a HIGH signal is anything from 3.0v through 5.5v when the micro-controller is powered by 5v. The main thing to note is that any voltage between 1.5v and 3.0v will be undefined and should be avoided to prevent unexpected application execution. Using our last example, I am now going to create a circuit and sketch that will only trigger when the temperature reaches a certain value such as 80°F which would be 26.67°C.

* the Arduino® UNO runs on 5 volts hence the value when set to HIGH will be 5 volts. Other boards, such as the , run at a lower voltage such as 3.3v so the output voltage when set to HIGH will be equivalent to that lower voltage.

TEMPERATURE COMPARISON

First we will develop a circuit that will trigger when a certain temperature is reached. For this circuit, we will want it to trigger at 80°F which would be 26.67°C. Using what we know about the

LM335, 0°C is equal to 2.73v and each 1°C increases the voltage by 10mV. If we divide the 26.67°C

by 100 to get it into the 10mV range, we will see that we need a voltage increase from 0°C of 0.2667v. So our target voltage across the LM335 will be 2.9967v. Oddly enough this is almost enough voltage to trigger our digital input however due to the variations found in components, doing this would be problematic and result in flaky operation of our program and circuit. To overcome this, we will create a comparison circuit that will trigger the digital HIGH value in the required range which is above 3.0v.

REQUIRED COMPONENTS

Arduino® Board 4.7K Ohm Resistor

(2) 10K Trim potentiometers

LM335 temperature sensor[ ] or similar such as the NTE7225[ ] LM311 voltage comparator[ ] or similar such as the NTE943M[ ]

TOOLS

Arduino Due

7 8

(46)

breadboard jumper wires voltage meter

SCHEMATIC

For this schematic we will be using the circuit from before however this time we will be using a voltage comparator to compare our temperature voltage with our target voltage. As noted above, our target is 2.9967v. First we will need to wire up a voltage dividing circuit. We will want to use a 4.7K Ohm resistor in series with a 10K potentiometer. This will allow us to adjust the voltage across the potentiometer to our target voltage. In order to fine tune the target voltage, we can do one of two things. First we can use the analog pin of our Arduino® to read in the voltage drop across our trim potentiometer. This will require a little coding but will work fine. The other option is to use a

multimeter to monitor the voltage across the potentiometer and fine tune it using that reading. Either is fine depending on what tools you have available. This connection will be wired into the voltage comparator. For the second half of our circuit, will be connecting the LM335 temperature sensor into the other input of the voltage comparator which will trigger an input of HIGH when our target voltage is reached. It will drop back to LOW once the temperature cools down.

Image created using:

SOFTWARE

The following is the code that is necessary to monitor the input signal to determine if our

temperature reading is greater than our set point, and if so, take an appropriate action such as turning a fan on.

/*

* This circuit is designed to toggle between a HIGH signal and a LOW * based on the current temperature in the room. The potentiometer * and the resistor are used to divide the 5v signal into two values

(47)

* giving us the comparison we need to trigger the comparator output. * 2.73v is 0°C so to get the comparator to trigger we will want

* to ensure that there is 2.73v + X across the potentiometer where * X is the temperature we want to toggle on.

* The temperaturePin (now called setPointPin) will again read in the * temperature however it will actually be the voltage across the * potentiometer so we can use that as our setpoint.

* The temperatureTogglePin will be used to determine if we are above * or below our target temperature.

*/

const int setPointPin = 0;

const int temperatureTogglePin = 7;

const float baseVoltage = 2.730f; /* 2.730V at 0°C */ const float voltageReference = 5.0f;

const float maxSampleCount = 1023.0f; const float CtoFConversion = 9.0f / 5.0f; const float FtoCConversion = 5.0f / 9.0f; const float CtoFDelta = 32.0f;

/*

* Method declarations */

float getTemperature(float temperatureVoltage);

float convertTemperatureToF(float temperatureInCelsius); float convertTemperatureToC(float temperatureInFahrenheit);

/*

* getTemperature - determines the temperature in celsius based on the * incoming voltage value assuming 2.73v equals 0°C

*

* Params - temperatureVoltage - the incoming voltage representing the temperature

*

* Returns - float - the actual temperature in celsius based on the incoming voltage

*/

float getTemperature(float temperatureVoltage) {

float currentTemperature = 0.0f;

// From the datasheet, every 10 mv is equal to 1 °K - 0°C is 273°K equal to 2.73v

// Temperature is scaled by 100 to bring it into the celsius scale from kelvin

currentTemperature = (temperatureVoltage - baseVoltage) * 100;

return currentTemperature; }

/*

* convertTemperatureToF - converts the temperature from Celsius to Fahrenheit

*

(48)

*

* Returns - float - the temperature expressed in Fahrenheit */

float convertTemperatureToF(float temperatureInCelsius) {

float convertedTemperature = 0.0f;

convertedTemperature = (temperatureInCelsius * CtoFConversion) + CtoFDelta; return convertedTemperature; } /*

* convertTemperatureToC - converts the temperature from Fahrenheit to Celsius

*

* Params - temperatureInCelsius - the incoming temperature in Fahrenheit *

* Returns - float - the temperature expressed in Celsius */

float convertTemperatureToC(float temperatureInFahrenheit) {

float convertedTemperature = 0.0f;

convertedTemperature = (temperatureInFahrenheit - CtoFDelta) * FtoCConversion; return convertedTemperature; } /*

* Setup - main configuration point of our sketch to configure * the Arduino for analog input

* * Params - none * * Returns - nothing */ void setup() { pinMode(temperatureTogglePin, INPUT); analogReference(DEFAULT); Serial.begin(9600); } /*

* loop - main method which will be called each time the * Arduino goes through its main loop to read the * setPoint temperature value and check our digital * input pin to see if it is high or not.

*

(49)

* * Returns - nothing */ void loop() { int rawSetPointValue = 0; float setPointVoltage = 0.0f; float temperatureSetPoint = 0.0f;

// Read in the temperature value which will be in a range from 0 to 1023 rawSetPointValue = analogRead(setPointPin);

// Convert our incoming voltage representation to a value we can utilize setPointVoltage = ((float)rawSetPointValue * voltageReference) /

maxSampleCount;

// We now have the current temperature expressed in celsius temperatureSetPoint = getTemperature(setPointVoltage);

// Print out the current temperature in both C and F Serial.print("The temperature set point is: ");

Serial.print(temperatureSetPoint); Serial.print("C which is "); Serial.print(convertTemperatureToF(temperatureSetPoint)); Serial.println("F"); if (digitalRead(temperatureTogglePin) == HIGH) {

Serial.println("We have reached our set point."); }

else {

Serial.println("Set point has not been reached."); }

// Rest for a second and check again delay(1000);

}

A significant amount of the code above is straight from the earlier example which will now be used to monitor the set point temperature and allow us to tweak the value as the program is executing. Once we have the set point established we can than vary the temperature that our LM335 is reading by holding our finger on it to warm it up or using a fan to cool it off. While I won't rehash everything that was done before, I will highlight the main differences that were made.

const int setPointPin = 0;

const int temperatureTogglePin = 7;

I changed the the temperaturePin variable name to setPointPin which better describes what this constant variable is doing i.e. defining the pin on the Arduino® that we will read for the set point temperature. The second constant variable is temperatureTogglePin which is the pin on the

(50)

Arduino® which we will monitor to detect if the LM335 has reached the setPoint or not.

/*

* Setup - main configuration point of our sketch to configure * the Arduino for analog input

* * Params - none * * Returns - nothing */ void setup() { pinMode(temperatureTogglePin, INPUT); analogReference(DEFAULT); Serial.begin(9600); }

The next major change is in the setup method where I am now configuring the temperature

toggle pin to be a digital input pin. The method, pinMode, instructs the Arduino® to configure this pin for digital input data. Remember that most pins on the Atmel® micro-controller, which is what the Arduino® utilizes, can be configured to perform a number of disparate functions. In order to use these functions, we must ensure the micro-controller is configured properly before we start running our code.

/*

* loop - main method which will be called each time the * Arduino goes through its main loop to read the * setPoint temperature value and check our digital * input pin to see if it is high or not.

* * Params - none * * Returns - nothing */ void loop() { int rawSetPointValue = 0; float setPointVoltage = 0.0f; float temperatureSetPoint = 0.0f;

// Read in the temperature value which will be in a range from 0 to 1023 rawSetPointValue = analogRead(setPointPin);

// Convert our incoming voltage representation to a value we can utilize setPointVoltage = ((float)rawSetPointValue * voltageReference) /

maxSampleCount;

(51)

// We now have the current temperature expressed in celsius temperatureSetPoint = getTemperature(setPointVoltage);

// Print out the current temperature in both C and F Serial.print("The temperature set point is: ");

Serial.print(temperatureSetPoint); Serial.print("C which is "); Serial.print(convertTemperatureToF(temperatureSetPoint)); Serial.println("F"); if (digitalRead(temperatureTogglePin) == HIGH) {

Serial.println("We have reached our set point."); }

else {

Serial.println("Set point has not been reached."); }

// Rest for a second and check again delay(1000);

}

In the main loop of our sketch, I have changed the name of a number of variables to better describe their function. I have also added in code to read the state of the temperature toggle pin to determine if we have reached our set point or not. This is done by calling the aptly named

digitalRead method. This method takes one argument which is the value of the pin to be read. It will

return either a HIGH or LOW value depending upon the voltage being read at the input pin of the device. For now I am simply displaying that I have either reached the set point or not. Later on we can take additional actions to do something a little more useful with this information. We will also look at another form of digital input when working with later on.

Summary

The Arduino® provides pins to read in both analog and digital data. The analog data can be any signal such as a temperature reading, photo-resistor, or any other such item that provides a range of values based on an environmental stimulus. Digital inputs can be used to determine button presses, on and off states, and other binary types of data. Together, both of these types of inputs can be used to monitor the world around us and allow us to react to events when they occur.

(52)
(53)

References

Related documents

Social media has negative effects on the lives of children, not only with an increase in gender stereotyping, but specifically in sexualizing men and women, as well as creating these

Remember the holes next to the Arduino pins are already connected to the Arduino pins so you don’t need to bend the wire and solder it directly to the pin... Connecting the Arduino

AnalogRead Reads the value write the specified analog pin Arduino boards contain a multichannel 10-bit analog to digital converter This color that review will map input voltages

Note: When using an Arduino Shield with a Dragonfly, install the SIM card in the Dragonfly and then install the Dragonfly on the developer board before installing the Arduino

12 significantly enriched pathways shared by both diseases can be divided into two groups: the neurodevelopment-related pathways (including neuroactive ligand-receptor

Sixth, all inward-facing activities that help transfer knowledge, initiate learning processes, motivate employees, stimulate their innovative behavior, and select the most

In spite of having a different energy profile from developed countries, it is important for Brazil to keep stimulating the use of renewable sources in energy production, taking

 Despite the development of multilateral regimes, states remain central actors in global governance; the international system remains fundamentally a system of sovereign states..