Home

Arduino-Based Dataloggers: Hardware and Software

image

Contents

1. example for single ended operation at a GAIN ONE setting the resolution is 4 096 2 2 8 192 32767 0 250 mV and the conversion from integer values from O to 32767 returned when the inputs are polled is Ax 32767 4 096 You can test this calculation by connecting a 1 5V battery or some other known voltage source between one of the inputs and ground recording some data and checking 51 the conversion from integer values to volts use a voltmeter to monitor the actual voltage of the battery which will be above 1 5V when it is new Although the default gain setting of GAIN_TWOTHIRDS has a stated range of 6 144V in fact the voltage applied to any input channel should never exceed the power supply voltage SV for the hardware shown in Figure 5 Table 6 Interpreting ADS gain settings Gain setting re eR ADS1015 1115 resolution URS code ee iid conversion to volts a 6 144V 0 6 144V 3 mV 0 375 mV eee ree A GAIN_ONE 1 4 096V 0 4 096V 2 mV 0 250 mV Ea GAIN_TWO 2 2 048V 0 2 048V 1 mV 0 125 mV een GAIN_FOUR 4 1 024V 0 1 024V 0 5 mV 0 0625 mV ae GAIN EIGHT 8 0 512V 0 0 512V 0 25 mV 0 03125 mV ee ed ae GAIN SIXTEEN 16 0 256V 0 0 256V 0 125mV 0 015625 mV ee ae 6 Input channels with no voltage source connected will produce spurious and meaningless values They can be ignored of course and you don t even have to read values from unused channels but
2. logfile print now day logfile print logfile print now hour logfile print logfile print now minute logfile print logfile print now second logfile println logfile flush write to file endif ECHO_TO_FILE delay delay_t 5 byte maximum Arduino Uno on COMS Sketch 18 Write date and time data to SD card file at specified time interval Sketch 18 includes a pre compile directive to turn off writing to a file for the purpose of testing the rest of the code All the code for writing data to a file is included between within the if endif directives The syntax for logfile print and logfile printin is the same as for Serial print and Serial printin However logfile print and logfile println don t actually write data to the file Those statements temporarily store data in a 34 buffer The logfile flush statement actually writes data to the file you can think of it as flushing the buffer by transferring data to the SD card file In principle this means that you can store the results of multiple calls to print or printin before actually writing those results to a file This may save processing time and memory space but it doesn t seem worth the effort for the kinds of data logging applications that will be dealt with in this document For Arduino programming file names are restricted to no more than 8 characters a period and a maximum of three cha
3. read the input pin HIGH if button pressed Serial println buttonState if buttonState HIGH digitalWrite LEDPin HIGH turn LED on when pressed else digitalWrite LEDPin LOW turn LED off delay l a little delay time between reads stabilizes the operations Binary sketch size 3 140 bytes of a 32 256 byte maximum Arduino Uno on COMS Sketch 14 Use LED to display state of a pushbutton 2 3 2 Analog pins Reading signals on analog input pins is no more difficult than reading digital pins except the return is an integer value whose interpretation is based on the input voltage relative to the reference voltage applied to the pin See http arduino cc en Reference AnalogReference from Reference AREF and http arduino cc en Tutorial AnalogInputPins The functions available for controlling and accessing analog pins include analogReference type Determines the reference voltage for analog input the value used as the top of the input range The allowed type values include DEFAULT 5V for 5V boards or 3 3V for 3 3V boards NTERNAL a built in reference of 1 1 V EXTERNAL a voltage applied to the AREF pin between 0 and 3 3V or 5V depending on the board Additional TYPE values are not available on the Arduino R3 or equivalent boards The reference voltage for all analog pins is set with analogReference you cannot set different values simultaneously for different pins although you can write code to chang
4. Serial print sq x Serial println sq x Serial print square x Serial println square x 10 Serial print hypot x y Serial println hypot x y 10 Serial print sqrt x Serial printlni sqrt x 10 Serial print cos x Serial println cos x 10 Serial print sin x Serial println sin x 10 Serial print tan x Serial println tan x 10 Serial print acos x Serial println acos x 10 Serial print asin x Serial println asin x 10 Serial print atan x Serial println atan x 10 Serial print atan2 x Serial println atan2 x y 10 Serial print cosh x Serial println cosh x 10 Serial print sinh x Serial println sinh x 10 Serial print tanh x Serial println tanh x 10 Serial print fmod x y Serial println fmod x y 10 Serial print fabs x Serial println fabs x 10 Serial print floor x Serial println floor x 10 Serial print trunc x Serial println trunc x Serial print ceil x Serial println ceil x 10 Serial print fmin x y 7Serial println fmin x y 10 Serial print fmax x y Serial println fmax x y 10 Serial print round x Serial println round y 10 Serial print isnan x Serial println isnan x void loop v gt 5 256 bytes of a 32 256 byte maxim Arduino Uno on COMS Sketch 8 Examples of Arduino math functions 18 2 2 7 Arrays An array is a collection of va
5. break default Serial println Oops no such gain setting return Serial case 4 adslll Serial case 8 adslll Serial CAT ATA ATG oO n 0 do wait until multiple of dtSave minutes 0 sec DateTime now RTC now Minute now minute Second now second while Minute SdtSavetSecond 0 void loop DateTime now RTC now Year now year Month now month Day now day Hour now hour Minute now minute Second now second if intervalType s if Second dt 0 dataOutput 58 else if Minute dt 0 dataOutput delay delay t Don t process the same second twice Figure 12 shows data from one channel of an ADS1115 board collected using Sketch 25 The data are from a pyranometer http www instesre org construction pyranometer pyranometer htm which measures incoming solar radiation Sampling starts at a multiple of 5 minutes with sampling every 10 seconds thereafter and statistics generated every 5 minutes Initially the pyranometer is in shadow It emerges from the shadow at around 24 39 days The sky was partly cloudy with scattered to broken cumulus which explains the large swings between maximum and minimum values and the corresponding swings in standard deviation Recall the comments about standard deviation calculations in the disc
6. integer variables Note the use of bbe lcan piniigi talee scientific notation with e or E for expressing real numbers Variables can be converted from one type to another For example it might be desirable to convert an integer value into its corresponding floating point value or an integer to a long integer A char conversion will convert an integer value to its corresponding ASCII character Applying int to a character will return the ASCII value for that character Boolean variables true or false are represented by one byte integer values equal to 1 or 0 There is no built in data type for strings of characters in Arduino programming There are two ways to construct strings There is a String object which includes methods for creating and manipulating strings See http arduino cc en Reference StringObject for more information A simpler but less flexible method is to define a string as an array of characters See 2 2 7 Arrays for more information about arrays Creating a string as an array of characters requires less computing resources than the String object and should be used unless you actually need String object methods Sketch 2 shows some examples of data type conversions and strings Note that strings constructed as an array of characters do not have to be displayed one character at a time in a for loop See Section 2 2 3 Conditional and repetitive execution Look up a table of ASCII characters to check the character
7. sais define Give a name to a constant value to be used some hardware specific language A E throughout a sketch No equal sign or components may be unfamiliar semicolon line terminator The Arduino language makes use define PI 3 14 include Include external libraries No semicolon line of the object concept In programming terminator objects are code constructions which include lt RTClib h gt define attributes that describe const A keyword to mark defined variables as properties of the object and methods read only The value of such variables cannot be redefined later in the code Usually preferred over define const float x 3 333 which define procedures associated with the object For example in Arduino programming there is a Serial object M which includes methods for displaying x 0 Not allowed output on and reading output from the serial port for example Serial print and Serial read as will be described below In general you do not need to know anything about how programming languages define and implement objects it is enough to understand how to use them in their proper context If you wish to make use of Arduino s capabilities there is no substitute for understanding its language and writing your own code If you have no previous programming experience you can learn a lot about programming in general and Arduino programming in particular by studying the examples in this document a
8. Rechargeable batteries are 99 Battery voltage in tempting but expensive From the same source a single rechargeable 7 0 0 0 0 5 1 0 1 5 2 0 2 5 3 0 D cell was 10 95 and a charger for up to four D cells was 27 95 A 9 V rechargeable battery would Time days Figure 13 Battery voltage from 6 Energizer alkaline D cells with Uno R3 Adafruit data logging shield and ADS1115_ board work for short term testing 6 75 louis avery 30 secon for a NiMH rechargeable plus 8 for a two battery charger from www batterymart com but these batteries will not last long in the field It might be tempting to use 8 1 5 V batteries in series for an initial voltage of a little more than 12 V when the batteries are new so you wouldn t have to change batteries so often But I would avoid this temptation When you connect an external power supply to the Arduino Uno using the 2 1 mm jack that voltage is delivered to a 7805 5 V regulator on the Uno board So called linear regulators like the 7805 are not very efficient devices Excess power input voltage 5V scurrent must be dissipated as heat by the regulator and board That is why some versions of 780x regulators have a connection for an external heat sink see the tab in the example shown below in Figure 14 Even though the 7805 regulators on Arduino boards are rated for an input up to 18V excess heat from an input greater than 12V can dest
9. Serial print Hour Serial print Serial print Minute Serial print Serial print Second Serial print Serial print Dayt Hour 24 Minute 1440 Second 86400 5 Serial print Serial print adc0 Serial print Serial print adcl Serial print Serial print adc2 Serial print Serial print adc3 Serial printin endif ECHO TO SERIAL if ECHO_TO FILE ogfile print Year logfile print logfile print Month logfile print logfile print Day logfile print logfile print Hour logfile print logfile print Minute logfile print logfile print Second logfile print logfile print Day Hour 24 Minute 1440 Second 86400 5 logfile print logfile print adc0 logfile print logfile print adcl logfile print logfile print adc2 logfile print logfile print adc3 logfile printlin logfile flush write to file endif ECHO TO FILE void setup Serial begin 9600 getConfiguration Serial print logging interval Serial print dt Serial printin intervalType 53 Serial print Log to file Serial print
10. begin 9600 int x 175 float y if x lt 100 Serial println x y 10 else if x lt 200 Serial printin x is OK y 20 else if x lt 300 Serial println x is high y 30 else Serial println Oops x is out of range y 0 void loop Arduino Pro of Pro Mini 6V 16 MHz uw A Sketch 3 Example of i f construct 10 SEE ForLoops Arduino 1 0 5 r2 DER File Edit Sketch Tools Help ForLoops void setup Serial begin 9600 int i for i 0 i lt 10 i Serial println i Serial print i after loop termination Serial println i for i 14 i gt 0 i 3 Serial println i 10 O oJ f wonre Oo i after loop termination 11 Serial print i after loop termination Serial println i int row col for row l row lt 10 rowt t for col 2 col lt 5 col Serial print row col Serial print Serial println after loop termination l 23 4 5 4 6 8 10 12 15 68 12 16 20 10 15 20 25 12 18 24 30 14 21 28 35 16 24 32 40 18 27 36 45 10 20 30 40 50 void loop lt OoJ fFPwonre NY oH Ow on wo Arduino U Sketch 4 Some examples of for loops The integer loop control variable knt you can name it whatever you want is assigned an initial value It is then incremented or decremented as specified and continues to be incremented or decremented as long as its value meets the d
11. output from a sketch text or numerical values can be displayed by sending the output to a serial port with Serial print and Serial println Here is a summary of those and other methods Serial begin baud rate Opens a serial port Example Serial begin 9600 9600 is the baud rate for many applications 15 Serial peek reads a byte in the serial buffer but doesn t move past that character One use of this method is to wait until a user presses a key in the serial port monitor window See Sketch 5 Serial print x n Serial print str Displays value of x or a string in the serial port monitor The optional parameter n specifies the number of digits to the right of the decimal point to include in the display of a floating point number The default value without specifying n is two digits to the right of the decimal point Serial println x n Serial printin str Like Serial print but appends an end of line mark to the output Example Serial print Here is some output Serial printin 3 333 6 The serial port monitor will display Here is some output 3 333000 As is the case with other high level programming languages the process whereby numerical values are translated into printable characters is interesting but almost certainly of no concern to users of the language Serial read reads and returns a byte in the serial buffer and moves to the next byte Ser
12. print Serial print printin Serial Serial c0 DtoA adsi1115 mX0 adc0 sumXX0 adc0 adc0 cl DtoA ads1115 mX1 adcl sumXX1l adcl adcl c2 DtoA ads1115 mX2 adc2 sumXX2 adc2 adc2 c3 DtoA ads1115 mX3 adc3 sumXX3 adc3 adc3 T adc0 gt max0 max0 adc0 if adcl gt max1 adc2 gt max2 max2 adc2 if adc3 gt max3 f ECHO TO SERIAL Serial print Year Serial print Serial print Hour Serial print Serial print Serial print adc0O 5 Serial print adcl 5 Serial print adc2 5 Serial print adc3 5 ndif ECHO_TO SERIAL Protect against small negative values possible with real number arithmetic under some conditions when the values don t change during sampling interval std_dev0 sqrt ma std_devl sqrt std_dev2 sqrt ma std_dev3 sqrt max x max x L L L 0 0 0 0 aana a r sumXX0 sumX0 sumX0 N N 1 sumXX1 sumX1 sumX1 N N 1 sumXX2 sumX2 sumX2 N N 1 sumXX3 sumX3 sumX3 N N mean0 sumxX0 N meanl sumX1 N mean2 sumX2 N if ECHO TO SERIAL Serial print Year Serial print Serial print Hour Serial print Serial print Serial print Serial print Serial print min0O 5 Serial print Serial print Serial print Serial print min1 5 Serial print Seri Serial print Day Seria Serial print Seri
13. the built in math functions display 16 in orange font but other functions don t For example the log in log x is displayed in orange font but the log10 in 10g10 x isn t Why I don t know All that matters is that both functions work Note that PI uppercase is a built in defined constant As is the case for C based and many other languages the names of everything are case sensitive so pi is not the same as PI See Sketch 5 above for an example of using Arduino s random number generator The random min max function generates pseudo random long integers optionally between specified minimum and maximum values randomSeed i where i is an integer value causes the random number always to start at the same point in its sequence depending on the value of i Pseudo random numbers are not really random They are generated by an algorithm based on an initial value a seed in a way that the resulting numbers should pass statistical tests for randomness See http arduino cc en Reference RandomSeed for more information on these two functions including how to start the random number sequence at a different position every time the sketch runs All trigonometric functions accept as input and return as output angles in radians not degrees radians degrees x 2 180 and vice versa to convert radians back to degrees sin 30 will cause no problems when you compile your sketch but if you really want the sine o
14. to integer and integer to character conversions done in Sketch 2 for example the character c has a base 10 ASCII value of 99 Table 3 gives math operators along with their Table 3 Math operators compound versions The assignment operator looks like the Math operation Operator algebraic equals sign but its interpretation in ETE compound operator programming is entirely different It means Evaluate an M ddition nm expression on the right side of the assignment operator and subtraction E assign that value to the variable name on the left side of the multiplication F assignment operator Hence division f integer modulo int x 3 x x 3 makes no algebraic sense x cannot be equal to itself plus 3 but makes perfectly good sense in programming When these two statements are executed x has a value of 6 Compound operators provide a shorthand method for certain arithmetic operations For example int x 3 x 3 is completely equivalent to the previous statements DatalypeConversions Arduino 1 0 5 r2 File Edit Sketch Tools Help char to int 99 DataTypeConyersions int to char C void setup Arduino f put your setup code here to run once string as an array of characters int a 67 char b c String d Arduino char e string as an array of characters Serial begin 9600 Serial print char to int Serial printin int b Serial prin
15. year month day hour minute second day_frac light T_C 2 2014 6 26 15 1 55 26 62633 844 27 34 3 2014 6 26 15 2 0 26 62639 839 27 34 4 2014 6 26 15 2 5 26 62645 840 27 34 5 2014 6 26 15 2 10 26 62651 839 27 34 6 2014 6 26 15 2 15 26 62656 845 27 34 7 2014 6 26 15 2 20 26 62662 864 27 34 8 2014 6 26 15 2 25 26 62668 926 27 34 9 2014 6 26 15 2 30 26 62674 930 27 34 Figure 7 b SD card file TEMPLITE CSV output for MyLightTemperatureLogger ino 3 1 4 Additional software considerations for a data logging application For a general purpose data logging application it is useful to be able to modify its performance with configuration parameters The simplest implementation samples and stores data at a specified interval Sketch 19 is an example of such an implementation A more flexible implementation would allow changing the sampling interval without having to make changes to the code It might also be desirable to sample data at some specified interval and then at some longer interval calculate and store statistics for the individual samples For example sample at 10 second intervals and then at 5 minute intervals calculate and store average max min and standard deviation for those 30 samples 37 There are two possibilities for providing additional flexibility One is to create a configuration file offline and store it as a text file on the same SD card used to store data Another way is to upload the sketch
16. 1 Hardware Although the code development in 3 4 stands on its own for many purposes an Arduino based datalogger is useful only if it is possible to attain a digital resolution much better than what is available with the built in 10 bit Arduino A D conversion Fortunately the four channel 16 bit ADS1115 board mentioned at the beginning of this document https learn adafruit com adafruit 4 channel adc breakouts makes this very easy First download and install the required Adafruit _ADS1015 cpp and Adafruit ADS1015 h files from https github com adafruit Adafruit_ADS1X15 and install them in their own folder libraries Adafruit ADS1015 Even though the names of these files imply that they are for the 12 bit ADS1015 board they also contain code for the 16 bit ADS1115 board The web page source for the library files includes an example sketch for testing the ADS1115 board and there is no reason to duplicate it here 49 The Adafruit data logging shield will still be used for its real time clock and SD card interface but unlike the applications discussed in Section 3 1 the voltage outputs from sensors are connected to the ADS1115 inputs rather than directly to the Arduino s analog input pins This project will use single ended inputs for four channels rather than differential inputs for two channels The Arduino handles all the communications with the ADS1115 and its four input channels through the SCL clock and SDA data pins It is
17. 29 0 3 inherent accuracy of the 300 28 5 2 measurement providing the analog 7 200 28 0 signal 100 27 5 The light intensity is in o l l l 27 0 23 6 23 65 23 7 23 75 23 8 23 85 23 9 arbitrary units The logger was Fractional days July 23 2014 placed on my indoor office i f Figure 9 Recorded output from Sketch 22 2 m intervals window ledge in the afternoon it is rather warm there in the afternoon sunlight and it is easy to see when as the light faded in the early evening I turned on the office light and then later turned it off when I left the office Sketch 22 is listed here in its entirety Sketch 22 MyLightTempLoggerB ino include lt SD h gt include lt Wire h gt 42 include lt RTC l define ECHO 1 define ECHO 1 input example File logfile int Second del char intervall int tempReading int dt char m or c 5 Minimum sampling interval Lib h gt TO FILE 0 TO SERIAL 1 12 s logfilel the 1 lay t 1000 dt lype le 1 photocellRe outFil float temperatureC CSV char file name xxxxxxxx xxXX 2s ogging file 3 ading Year Month Day Hour Minute const float aref voltage 3 3 const int photocellPin 0 tempPin 1 chipSelect 10 RTC_DS1307 RTC void getConfiguration char c int n 0 while Serial peek lt 0 dt Serial parseInt intervalType Serial read while Se
18. Arduino data logging shield with temperature and light sensors connected as shown above in Figure 4 Sketch 19 is a simple data logging program for these sensors These sketches and some that follow are too long to capture as screen shots of the IDE window It records temperature C and light sensor data integer values between 0 and 1023 every 5 seconds Using pre compile directives the output can be switched between the serial port set ECHO _TO SERIAL to 1 for testing as shown in the sketch and to an SD card file ECHO _TO FILE to 1 or both outputs can be turned You can name these directives whatever you like these just seemed like reasonable names Some serial port output is shown in Figure 7 a and some data from an output file opened in Excel is shown in Figure 7 b Sketch 19 MyLightTempLogger ino include lt SPI h gt include lt SD h gt include lt Wire h gt include lt RTClib h gt define ECHO TO FILE 0 define ECHO TO SERIAL 1 File logfile the logging file int Second delay t 1000 dt 5 35 int tempReading photocellReading Year Month Day Hour Minute float temperatureC const float aref voltage 3 3 const int photocellPin 0 tempPin 1 chipSelect 10 RTC_DS1307 RTC Define real time clock object void setup Serial begin 9600 pinMode 10 OUTPUT if ECHO TO SERIAL Serial printin Write to serial port Serial println year month da
19. Serial print Second Se dayFrac 5 Serial print mean0 5 Serial print S Serial print S meanl 5 Serial print S Serial print S 56 print Month print Minute al print L L L 1 mean3 sumx3 N al print Month Hee chan oy ee m ie al print Minute vrial print erial print max0 5 erial print std_dev0 8 erial print max1 5 erial print std_devl 8 Serial print Serial print Meant 5 Serial print Serial print Serial print Serial print min2 5 Serial print Serial prin Serial print Serial print mean3 5 Serial print Serial print Serial print Serial print min3 5 Serial print Serial prin endif ECHO_ TO SERIAL if ECHO_TO FILE logfile print Year logfile print logfile print logfile print logfile print Day logfile print logfile print Hour logfile print logfile print logfile print logfile print Second logfile print logfile print dayFrac 5 logfile print logfile print mean0 5 logfile print logfile print logfile print logfile print minO 5 logfile print logfile print logfile print logfile print meanl 5 logfile print logfile prin
20. Sketch 13 Blink a LED Sketch 14 uses the hardware shown in Figure 2 a pushbutton connected to a digital pin through a 10KQ resistor to ground The purpose of the resistor is to limit the amount of current that will flow from the pin to ground when the button is pushed this current should not exceed 40 mA From Ohm s law the current through the 10KQ resistor is I V R 5 10000 0 5 mA Do NOT connect a pushbutton directly from the pin to ground The digital pins are at the top right The blue wire is connected to pin 2 The red and black wires are connected to the 5V pin and ground In the code below the digitalRead function reads the state of pin 2 HIGH when the button is pressed or LOW For software help see these tutorials http arduino cc en Reference digitalRead and _http arduino cc en Reference digitalWrite If everything is working OK the small yellow LED marked with the red circle will light up only while the button is pushed and held down 25 ReadButton Arduino 1 0 5 r2 DAR Fie Edit Sketch Tools Help ReadButton int LEDPin 13 LED connected to digital pin 13 int inPin 2 external pushbutton connected to digital pin 2 int buttonState 0 variable to store the current button state void setup Serial begin 9600 pinMode LEDPin OUTPUT sets the digital pin 13 as output pinMode inPin INPUT sets the digital pin 2 as input void loop buttonState digitalRead inPin
21. You can define all the desired return values as global variables which are available inside any function There are two advantages to this approach 1 each value has its own variable name rather than being just an indexed array element 2 the values don t have to have the same data types The only disadvantage is that you have to be careful not to redefine those variable names elsewhere in your sketch This might be a significant problem in an environment with less restrictive maximum code size requirements but it shouldn t be a problem for the code that the Arduino can handle There is a third option for returning multiple values from a function This involves passing a pointer to a variable name and modifying the contents of the memory to which that pointer points In that case the function doesn t actually return anything so its type is void Sketch 12 shows an example 21 StatisticsFunction Arduino 1 0 5 r2 File Edit Sketch Tools Help Statistics for this array float A 4 A is a global variable Mean 0 797 void GetStats float x int n Standard deviation 0 266 float maxX 3 4028235E 38 minxX 3 4028235E 38 Maximum 1 300 float mean std_dev sum_x 0 sum_xx 0 for int i 0 i lt n i if x iJ gt maxX maxX x i if x i lt minX minX x i sum_x x i sum_xx x i x i std_dev sqrt sum_xx sum_x sum_x n n 1 mean sum_x n A O mean A l std_dev A 2 maxxX A 3 minx Stati
22. a situation where moisture can condense on a pc board must be avoided as this will almost certainly destroy the board There are commercial enclosures for Arduinos but they can be relatively expensive and they are certainly not necessarily weatherproof A simple plastic enclosure from Sparkfun PRT 10088 cost 12 in July 2014 compared to only 15 for an Arduino Pro board It is possible to make your own enclosures much more cheaply using for example standard plastic household electric outlet boxes and covers from your local home supply or hardware store You will have to cut holes for the USB cable and power supply a Dremel tool is useful for this although it can be done with a drill and sharp knife You could also use a snap top plastic sandwich container In any case for outdoor use it is a good idea to fill any container with something which will absorb moisture such as uncooked rice or cat litter 4 3 Powering Your Arduino Datalogger As noted previously Arduino boards and their accessories require a lot of power compared to something like the four channel Onset Computer Corporation UX120 006M loggers which will run continuously for months on two AAA batteries www onsetcomp com products data loggers ux120 006m This isn t a problem for indoor applications near a computer because any of the projects described in this document will happily run on power supplied through a USB port But what about applications away from a power
23. and enter the configuration data from the keyboard in the serial port monitor window Both methods require some knowledge about how Arduino reads and processes numerical and text input Sketch 20 shows one way to read data from a file on the SD card A lot of the code is required just to check the status of the hardware The rest of the code deals with interpreting data stored in the file When a data file is opened the default for an open file is read only the code establishes a pointer to the beginning of the file The read method reads the byte at that position and advances the pointer to the next byte This is not helpful behavior for interpreting several bytes in a file as numbers or words strings of characters It is important to understand that if you write 3 14159 in a text file it looks to you like the number 3 14159 But from a computer s point of view this is just a string of bytes that happens to represent digits and a decimal point The Arduino programming language includes two methods to deal with extracting numbers from strings of bytes parseInt and parseFloat These functions start at the current location of the file pointer They then look for the first byte that could be a character associated with a number the digits 0 9 a period decimal point for a floating point number a or character or e or E for a floating point number expressed in scientific notation That byte is the first character in a
24. be met by even a small 12 V lead acid battery kept on a solar charger The one shown in Figure 16 is described as 1 2 Ah despite the URL which says 1_3ah The performance of lead acid batteries is very temperature dependent They should be kept charged and not subjected to deep discharges a condition that shouldn t be a problem in this application Once in use lead acid batteries should not be removed from a charger for long periods of time Treated properly they will last for a long time Be careful about trying to use a solar panel voltage controller such as the SCN 3 from www allelectronics com 29 95 These devices have one pair of input terminals for the solar panel and two pairs of output terminals one for the battery and another for the load The load terminals may automatically turn off if the battery falls below 12V These devices should work OK if you make connections to the battery out terminals as described above The current draw even from a relatively inefficient 9 V regulator powering an Arduino project is small enough that it should not prevent the battery from charging in sunlight when it is connected to an Arduino board 63
25. debug Often for loops are used to access array elements For an array with 10 elements the appropriate index values are 0 through 9 not 1 through 10 Reading from element 10 in a 10 element array will not produce an error but it will produce junk whatever happens to be in that memory location at the time Trying to assign a value to element 10 in a 10 element array could destroy values in memory that you really didn t want to lose Sketch 9 shows a typical calculation performed on an array of numerical values find the mean and standard deviation of values in the array This code will be of interest in a datalogger sketch 2 2 8 User defined functions User defined functions serve two important purposes They make it easier to organize code and they facilitate calculations that must be done more than once but with different input values The two important points to know about functions is that 1 variables defined within a function local variables are isolated from variables in your main code or in other functions 2 functions can have multiple inputs but they can return only one value Sketch 10 shows a computationally trivially simple example Write a function which accepts the radius of a circle as input and returns the circumference as output If the function returns a value the data type must be included in the function definition If it doesn t return a value its function type should be void For this example the return value
26. for sensing light CdS photoresistor and temperature Analog Devices TMP36 see https learn adafruit com tmp36 temperature sensor is used here see http www adafruit com products 249 This is an instructive choice of hardware The photoresistor requires that current flow through it supplied by the Arduino board The temperature sensor requires power supplied by the Arduino The datalogger shield without any input sensors is preassembled Connecting the light and temperature sensors requires a little soldering as shown in Figure 4 but there are instructions in a complete user s guide https learn adafruit com downloads pdf adafruit data logger shield pdf Figure 4 a Connections for Figure 4 b Components installed in the work area of an Adafruit temperature and light sensors datalogger shield mounted on Uno R3 board As shown in Figure 4 the outputs from these two sensors are connected to analog pins AO and Al the two blue wires in the lower right hand corner in Figure 4 b the shorter wire passes just over the 30 left hand side of the green LED They make use of the built in 10 bit analog to digital conversion capabilities of the Arduino board to read the output These sensors are very useful for learning how to program a datalogger but the relatively low 10 bit ADC conversion resolution on the Arduino analog input over a 0 5V default input range 5 1023 5 mV will not be suitable for sensors wit
27. is a floating point number The data type s of the input parameter s must be specified as shown 19 Statistics Arduino 1 0 5 r2 SEE File Edit Sketch Tools Help Statistics for this array Mean 0 797 Statistics Standard deviation 0 266 void setup const int ARRAY SIZE 10 int i for loop counter float x ARRAY SIZEJ 0 5 77 1 3 8 99 0 53 62 55 81 1 1 float sum_x 0 std_dev sum_xx 0 for i 0 i lt ARRAY_SIZE i sum x x i sum xx x i x i std_dev sqrt sum_xx sum_x sum_x ARRAY_ SIZE ARRAY SIZE 1 Serial begin 9600 Serial println Statistics for this array Serial print Mean Serial printlin sum_x ARRAY_S5IZE 3 Serial print Standard deviation Serial println std_dev 3 void loop Binary Arduino Une on COMS Sketch 9 Using arrays There are many situations in which it would be desirable to return more than one value from a function For example suppose you wish the function in Sketch 10 perhaps renamed to CircleStuff to return both the circumference and area of a circle Arduino functions like C functions on which the Arduino programming language is based cannot return multiple values directly One way around this problem is to store multiple values in an array and to define that array as a global variable This is done simply by declaring the array before any other code in the sketch A globally declared variable should not be re declared
28. it can be a more efficient way to change values Arduino programming syntax for referencing and dereferencing pointers using amp and in front of variable names respectively is identical to the C language Oddly the Arduino programming reference http arduino cc en Reference Pointer is singularly unhelpful on this topic it basically advises you to look elsewhere Fortunately there are many online discussions of how to use pointers in C programming 2 3 Digital and Analog I O As noted previously a fundamental purpose of programming the Arduino is to control the hardware interface This is done through pins attached both literally and in the software sense to various 23 devices In this document we will just scratch the surface of this topic Just as there is no substitute for writing your own code to learn how to program there is no substitute for wiring up devices to the Arduino board Get some prototyping breadboards a few electronics components and some hookup wire and get started All these parts are widely available from places like RadioShack AllElectronics www allelectronics com and other electronics suppliers Or buy the Arduino starter pack mentioned in Chapter 1 Time spent with these simple devices absolutely will not be wasted in preparation for putting together a datalogger 2 3 1 Digital pins Digital pins can be set to either a high or ia PEERS low state See here for a tutorial on digital
29. it is simpler just to read all the channels It is a good idea to connect unused inputs to ground Sketch 24 HiResDataLogger ino Format for serial port window input int dt char m or s int gain 1 2 3 for 2 3 4 8 or 16 string xxxxxxxXX XXX include lt SD h gt include lt Wire h gt include lt RTClib h gt include lt Adafruit_ADS1015 h gt define ECHO TO FILE 0 define ECHO TO SERIAL 1 Adafruit_ADS1115 ads1115 File logfile the logging file int Second delay t 1000 dt chipSelect 10 gain char intervalType outFile 13 int Year Month Day Hour Minute float DtoA RTC_DS1307 RTC Define real time clock object void getConfiguration char c int n 0 while Serial peek lt 0 dt Serial parselInt delay 10 Serial read intervalType Serial read 52 delay 10 Serial read gain Serial parseInt delay 10 Serial read while Serial available delay 10 if Serial available gt 0 c Serial read outFile n c n outFile n J 0 void dataOutput intl6 t adc0 adcl adc2 adc3 adcO ads1115 readADC_ SingleEnded 0 adcl ads1115 readADC_SingleEnded 1 adc2 ads1115 readADC_ SingleEnded 2 adc3 ads1115 readADC_ SingleEnded 3 if ECHO TO SERIAL Serial print Year Serial print Serial print Month Serial print Serial print Day Serial print
30. number Sketch 15 shows how to use analogRead The two outer leads of a 10kQ potentiometer are connected to the 5V pin and ground The center lead is connected to an analog pin The code reads the integer value on the pin and blinks the LED on digital pin 13 at a rate that depends on the position of the potentiometer shaft with delay sensorValue having a value between 0 and 1023 milliseconds Sketch 15 also displays the voltage value sensorValue 1023 5 at the analog pin as shown in some output cut and pasted from the serial port window As noted above the decimal point after the 1023 is required The decimal point after the 5 is optional in this case For the Uno R3 board which operates at 5V it is essential not to apply a voltage outside the range 0 5 V to any pin digital or analog Exceeding this range will destroy that pin s function and may destroy the entire Uno board In Sketch 15 the voltage applied to the input pin is by definition some fraction of the 5V applied to the digital pin so an appropriate value is guaranteed This is a potential problem with an external sensor providing input to an analog pin Some Arduino boards are powered at 3 3V in which case that is the maximum allowed input voltage on a pin The input voltage on a pin is different from the supply voltage which powers the board that should be in the 7 12V range with a maximum 6 20V range The board contains an onboard voltage regulator to provide 5V to
31. print logfile print meanY 4 logfile print logfile print maxPhotocell logfile print logfile print minPhotocell logfile print logfile print std_devY 4 logfile printin flu Serial println year month day hour minute second day frac T mean 48 T max T min T stdDev Light mean PC_max PC_ min PC_stdDev endif ECHO TO SERIAL if ECHO_TO FILE Serial print Initializing SD card if SD begin chipSelect Serial printin Card failed or not present return else Serial printin card initialized char filename outFile logfile SD open outFile FILE WRITE if logfile Serial println Could not create file return else Serial print Logging to Serial println outFile logfile printin year month day hour minute second day frac T mean T max T min T stdDev PC_ mean PC_max PC_min PC_stdDev endif ECHO TO FILE Wire begin RTC begin analogReference EXTERNAL do DateTime now RTC now Minute now minute Second now second delay 10 while Minute 60 Second dtSave 60 0 void loop DateTime now RTC now Year now year Month now month Day now day Hour now hour Minute now minute Second now second if Secondsdt 0 dataOutput N delay delay t 3 2 A High Resolution Datalogger 3 2
32. print temperatureC 2 logfile printin logfile flush write to file endif ECHO TO FILE void setup void Serial begin 9600 getConfiguration Serial print dt Serial print logging interval Serial printin intervalType if intervalType s amp amp Serial print Serial print pinMode 10 OUTPUT Configuration error Log to file dt lt 2 Restart Serial printin outFile a r if ECHO TO SERIAL Serial printin Write to serial port Serial println year month day hour minute second day frac light T C endif ECHO TO SERIAL if ECHO TO FILE Serial print Initializing SD card if SD begin chipSelect Serial printin Card failed or not present return else Serial printin card initialized logfile SD open outFile FILE WRITE if logfile Serial println Could not create file return else Serial print Logging to Serial println outFile logfile printin year month day hour minute second day frac light T _C endif ECHO TO FILE Wire begin RTC begin analogReference EXTERNAL loop DateTime now RTC now Year now year Hour now hour if intervalType s if Second dt 0 if intervalType m if Minute dt 0 amp amp d
33. project lived on Now this open source hardware software system has spawned numerous cloned versions and given birth to a new and rapidly growing industry devoted to making use of its capabilities I believe it is reasonable to equate the impact of the microcontroller revolution in the early 21 century to the personal computer revolution at the end of the 20 century It might even be fair to conclude that being comfortable working with microcontrollers is as essential for any technically competent individual in the 21 century as personal computing skills which many of us now take for granted were in the late 20 century Because microcontrollers provide essentially unlimited opportunities for interfacing with hardware skills can be developed in many ways with projects ranging from the frivolous to the profound This document is not intended as a general purpose Arduino reference guide but only as a record with apologies for any errors from a newbie of the path I followed to develop an Arduino based high resolution datalogger I started with a scientific programming background but with absolutely no previous microcontroller programming experience The document includes many links to online sources which I found essential for acquiring the information I needed to reach my goal Here is the hardware used for this project Prices from www adafruit com and www sparkfun com are approximate as of July 2014 Quantity discounts are avail
34. this may or may not be true For example when a quantity is changing over time rather than fluctuating randomly as light levels and temperatures are likely to do the standard deviation calculated over some time interval may be interesting but it is no longer a standard deviation in the statistical sense If values are constant over time the standard deviation is 0 by definition However in that case real number math to calculate the quantity under the square root sign might result in a very small negative number instead of 0 which can cause an error when the sqrt function is used To avoid this error the value of the expression under the square root should be tested and the standard deviation assigned a value of 0 if that value is negative Figure 10 shows temperature and light intensity data recorded on the inside window ledge of my office with 10 second sampling and statistics calculated over 5 minutes The region with increased standard deviation for both temperature and light intensity corresponds to fluctuations due to changing cloud conditions For data like these the standard deviation is more properly interpreted just as a measure of the variability of the quantity being measured during a sampling interval The light intensity appears to be saturating in the bright sunlight that is the resistance of the CdS photoresistor is no longer changing linearly with light intensity One could experiment with different photoresistors to
35. within a function With no direct return value the function type should be void When you write the code it is up to you to keep track of which values are held in which array element A possibly significant restriction is that all the return values must be of the same data type because all the array elements must be of the same data type 20 CircleCircumference Arduino 1 0 5 r2 DER Fie Edit Sketch Tools Help CircleCircumference r 10 000 float GetCircumference float r circumference 62 632 float circumference PI 2 r return circumference oid setup float r 10 Serial begin 9600 Serial print r Serial println r 3 Serial print circumference Serial println GetCircumference r 3 oid loop Binary sketch size 3 972 bytes of a 32 256 byte maximum Arduino Uno on COMS Sketch 10 Example of a user defined function Sketch 11 is a rewrite of Sketch 10 with some statistics calculations done in a user defined function Because there are four calculated values mean standard deviation maximum and minimum all floating point numbers the results are returned in an array With this approach it is easy to add more return values to a function For example in Sketch 11 you could define the global array A with 5 elements instead of 4 and calculate the median in the function It is not necessary to use an array to access multiple values calculated within a function
36. 2 adc3 toA dayFrac float File logfile the logging file int N KNT 0 int delay t 1000 dt dtSave chipSelect 10 gain int Year Month Day Hour Minute Second char intervalType outFile 13 RTC_DS1307 RTC Define real time clock object void getConfiguration char c int n 0 while Serial peek lt 0 dt Serial parselInt delay 10 Serial read intervalType Serial read delay 10 dtSave Serial parseInt delay 10 Serial read gain Serial parseInt delay 10 Serial read while Serial available delay 10 if Serial available gt 0 c Serial read outFile n c n outFile n 0 if intervalType s N dtSave 60 dt else N dtSave dt void dataOutput 55 ad 6 oO Q Q N if if if if da i NRunuovn ono Nn e if readADC SingleEnded 0 readADC SingleEnded 1 readADC SingleEnded 2 readADC SingleEnded 3 adc0O lt min0O min0 a maxl adcl if adcl lt minl minl a adc2 lt min2 min2 a dco d l ac2 max3 adc3 if adc3 lt min3 min3 a dc3 yFrac DaytHour 24 Minute 1440 Second 86400 KNT N Serial print Serial Serial print Day Serial Serial print Serial print Serial print Second Seri dayFrac 5 Serial print Serial print
37. 4 July 2014 const int photocellPin 0 tempPin 1 chipSelect 10 RTC char c while dt Serial parseInt delay 10 tion delay 10 _DS1307 RTC Define real time clock object Read configura void getConfigura int n 0 Serial peek lt 0 tion parameters from serial port Serial read 46 50 w Oo Standard deviation 10 24 9 intervalType Serial delay 10 dtSave Serial parseInt delay 10 Serial read while outFile n 0 Serial available l read delay 10 Serial read delay 10 if Serial available gt 0 c Serial read outFile n c n N dtSave 60 dt Get data and display or log it void dataOutput int N float std_devx meanx std_devY meanY day frac a photocellReading analogRead photocellPin delay 10 tempReading analogRead tempPin delay 10 temperatu reC tempReading aref voltage 1023 0 5 100 sumX temperatureC sumXX temperatureC temperatureC float the square of the photocell reading to avoid integer overflow sumY photocellReading sumYY photocellReading float photocellReading KNT if temperatureC gt maxTemperature maxTemperature temperatureC if temperatureC lt minTemperature minTemperature temperatureC if photocellRe
38. Arduino Based Dataloggers Hardware and Software David R Brooks Institute for Earth Science Research and Education V 1 2 June 2015 2014 2015 An introduction to Arduino microcontrollers and their programming language with the goal of building a high resolution datalogger to record data from external sensors ae SOCO ONAA SOCCERO T Soin pee a T o DIGITAL Pai E E UNO lot Claes gt e eS aa Kenwtanage y z PEN SOUBCE ELECTRONICS pact DIV Ine PLATT cee ARDUINO ee IN ETA 5 ce ni s ay 4 3 ram on aS ec a sxeme ARDUINO C LIPO bee be ed i na gt 5 oe amp bo eom ee ys Oe s soe 10 he ane impatto reece This document makes use of code and other material from arduino com adafruit com and sparkfun com Arduino material is open source and in the public domain All other non original material is referenced as appropriate This document is protected under a Creative Commons Attribution NonCommercial NoDerivative 4 0 International License http creativecommons org licenses by ne nd 4 0 which means that it can be used for any non commercial purpose with attribution but cannot be redistributed with changes without permission from the author Please provide appropriate references for any such uses including IESRE s URL www instesre org I would appreciate knowing about such uses Please address questions and comments via e mail to brooksdr instesre org A file containi
39. E ARDUINO INTEGRATED DEVELOPMENT ENVIRONMENT IDE 2 1 Up and Running The Arduino project development environment or integrated development environment IDE is a free download for Windows Mac or Linux systems from http arduino cc en main software There is no point reading this document until you have installed the IDE software The work described in this document has been done on a Windows XP computer Once installed in an Arduino folder everything is in place to try some of the examples in the examples folder which will be created when you install the IDE Connect the Arduino board to your computer through a powered USB port or through a non powered USB port with an external power supply Note that the Arduino board uses relatively a lot of power compared for example to a commercial datalogger such as the Onset Computer Corporation s UX120 006M 4 channel voltage logger http www onsetcomp com products data loggers ux 120 series which will run for many months on two AAA batteries An Arduino plus some accessories will run continuously from a powered USB port but you will need a relatively hefty battery supply to run an Arduino continuously on its own for extended periods of time See the Section 4 3 for more details The arduino exe file opens the IDE with a window for writing code The source code for any Arduino application has a ino extension Every source code file is contained in its own folder with the same name
40. O stddev Al_mean Al_max Al_min Al stddev A2 mean A2 max A2 min A2_ stddev A3 _mean A3 max A3 min A3_ stddev endif ECHO TO SERIAL if ECHO_TO FILE Serial print Initializing SD card if SD begin chipSelect Serial printin Card failed or not present return else Serial printin card initialized logfile SD open outFile FILE WRITE if logfile Serial println Could not create file return else Serial print Logging to Serial println outFile no line breaks allowed in code Put print string all on one line logfile printin date time day frac A0 mean AOQ max A0Q min AO stddev Al_mean Al_max Al _min Al_ stddev A2 mean A2 max A2 min A2_ stddev A3 mean A3 max A3 min A3_ stddev endif ECHO TO FILE Wire begin RTC begin ads1l115 begin Serial print Gain setting switch gain case 1 ads1115 setGain GAIN ONE DtoA 4 096 32768 Serial println GAIN ONE break case 2 ads1115 setGain GAIN TWO DtoA 2 048 32768 Serial println GAIN TWO break case 3 ads111 setGain GAIN TWOTHIRDS DtoA 6 144 32768 rintln GAIN TWOTHIRDS break setGain GAIN FOUR DtoA 1 024 32768 rintln GAIN FOUR break setGain GAIN EIGHT DtoA 0 512 32768 rint n GAIN EIGHT break case 16 ads111 tGain GAIN SIXTEEN DtoA 0 256 32768 Serial println GAIN SIXTEEN
41. able e Arduino Uno R3 microcontroller ID 50 25 or Adafruit Arduino Uno R3 starter pack PID 68 65 00 e Adafruit data logging shield with light and temperature sensors PID 249 37 50 and Figure 1 Arduino Uno R3 microcontroller optionally additional data logging shields PID 1141 20 e Adafruit ADS1115 16 bit analog to digital conversion ADC board ID1085 15 e Sparkfun Arduino Pro microcontroller DEV 10915 15 e Sparkfun FTDI board DEV 09716 15 e A few other components for testing code as described in figures below http arduino cc en Main arduinoBoardUno contains a description of the classic Uno R3 board http www gammon com au forum id 11473 has a useful pin diagram The software component of any Arduino project requires some general programming knowledge plus details about Arduino hardware and the Arduino programming language If you already have experience programming with C C or related languages you may find much of this document to be tedious and you will be able to skip over large portions of it with perhaps some occasional detours here or onine to check on specific features of Arduino programming Arduino compatible boards and accessories like the Arduino Pro are available from www sparkfun com Some of these boards have the advantage of using less power than the Uno R3 but I recommend the Uno R3 board as a starting point to learn about using these microcontrollers 3 2 TH
42. ading gt maxPhotocell maxPhotocell photocellReading if photocellReading lt minPhotocell minPhotocell photocellReading day frac Dayt Hour 24 Minute 1440 Second 86400 if ECHO_TO SERIAL Serial print Year Serial print Serial print Month Serial print Serial print Day Serial print Serial print Hour Serial print Serial print Minute Serial print Serial print Second Serial print Serial print day frac 5 Serial print Serial print temperatureC 4 Serial print Serial print photocellReading Serial printin endif ECHO TO SERIAL if KNT N a sumXX sumX sumX N if a lt 0O a 0 std_devX sqrt a N 1 meanX sumX N a sumYY sumY sumY N if a lt 0O a 0 std_devY sqrt a N 1 meanY sumY N KNT 0 sumX 0 if ECHO_TO SERIAL Serial Serial Serial sumXX 0 sumY 0 sumYY 0 l print Year Serial print Serial print Month l print Serial print Day Serial print l print Hour Serial print Serial print Minute 47 Serial print Serial print Second Serial print Serial print day frac 5 Serial print Serial print meanX 4 Serial print Serial print maxTemperature 4 Serial print Serial print minTemperature 4 Serial print Serial print std_devx 4 Serial Serial print meanY 4 Serial print Serial print maxPhotocell S
43. as the ino file This folder is created automatically whenever you create a new code file In Arduino speak source code written using English math like instructions is called a sketch The name is based on The Arduino language s origins in Processing a programming environment for graphic design As a first example open the Blink ino sketch file found in the examples 01 Basics Blink folder Choose Upload from the file menu The code will be compiled into machine language and sent to the Arduino If there are errors messages will be displayed As is often the case error messages may or may not be helpful for fixing your code If everything is working the LED on the Arduino board should blink one second on and one second off If this simple sketch works it is an indication that software and hardware are working together as required Note that you cannot turn off a program once it is sent to the Arduino If you remove the power either by removing the USB cable or by unplugging a power supply the program will stop running But it is still in the Arduino s memory and that same program will start running again if you power up the board again It will stay in memory until you upload a different sketch Try it with Sketch 1 Sketch 1 is a modification of the Blink ino sketch It includes code which uses the Serial object to display output from a program in a serial port monitor window This is how you keep track of what you
44. change this performance 45 40 35 30 12 0 25 mo g 2 20 T average g T maximum 45 T minimum T standard deviation 10 5 0 24 4 24 5 24 6 24 7 24 8 24 9 Fractional day 24 July 2014 10 second sampling over 5 minutes Figure 10 a Temperature C Sketch 23 MyLightTempLoggerC ino include lt SPI h gt Hinclude lt SD h gt input example include lt Wire h gt Hinclude lt RTClib define ECHO TO FILE 1 define ECHO TO SERIAL 0 int dt char m or s int dtSave XXXXXXXX CSV 10 s 5 logfilel csv variables global float h gt float maxTemperature 100 minTemperature 150 maxPhotocell 0 minPhotocell 1023 int N KNT 0 File logfile int Second delay _ the char intervalType ou int tempReading phot float temperatureC const float aref logging file t 1000 dt dtSave tFile 13 tocellReading Year Month Day Hour Minute volt tage 3 3 o o ad js j gt fon co N e a Standard deviation deg C 9 P 9 N 0 0 sumX 0 sumY 0 sumXX 0 sumYY 0 Figure 10 b light intensity arbitrary units Light Intensity arbitrary units 1200 1000 800 600 400 200 0 l average l maximum l minimum l standard deviation 24 5 24 6 Fractional day 2
45. common anode LED Adafruit PID 159 in a diffusing 5 mm By varying the width of the voltage pulse supplied to each LED the visible output can be dimmed or turned off to select and blend colors quite smoothly the pulse frequency is high enough that the LED output doesn t appear to flicker The image shows the LED in its aqua mode In a simpler application analogWrite could be used to control the brightness of any LED otherwise this would have to be done by changing the resistor value Note never connect an LED to a pin without including a current limiting resistor For a typical LED this value shouldn t be less than about 270Q a standard resistor value For typical LEDs in 3 and 5 mm housings the current through the LED and resistor should be about 20 mA and no more than 30 mA for full brightness In many cases a smaller current will still provide adequate light and may be desirable to minimize power consumption 28 8 RGB1 Arduino 1 0 5 r2 DER int redPin 11 greenPin 10 bluePin 9 void setColor int red int green int blue for common anode LED red 255 red green 255 green blue 255 blue analogWrite redPin red analoglJrite greenPin green analogWrite bluePin blue void setup pinMode redPin OUTPUT pinMode greenPin OUTPUT pinMode bluePin OUTPUT void loop setColor 255 0 0 delay 1000 setColor 0 255 0 delay 1000 setColor 0 0 255 delay 1000 se
46. e rovide troubleshootin cient P 8 const int led 13 LED is off advice But extensive the setup routine runs once when you press reset LED is on online support is void setup LED is off available for this d initialize the digital pin as an output pinMode led OUTPUT widely supported open Serial begin 9600 source system I have registered with online he Ee loop routine runs over and over again forever void loop forums for Arduino and turn the LED on HIGH is the voltage level Adafruit customers digitalWrite led HIGH Serial println LED is on delay 1000 wait for a second post questions I have f turn the LED off by making the voltage LOW always gotten prompt digitalUrite led LOW Serial println LED is off delay 1000 wait for a second You have to register to and useful advice from these forums and other online sources whenever I encountered a problem The two most bytes of a 32 common reasons why a maximum successfully compiled Arduino Uno on COMS sketch won t work are 1 the correct Arduino Sketch 1 Turn an LED on and off board has not been selected 2 the wrong COM port has been selected These values are accessed through the Tools tab The current board and COM port values are shown in the lower right hand corner of the IDE window The COM port may be different for different boards even of the same model 2 2 The Arduino Programming Lang
47. e beginning if there is a space between a comma and a string of characters The output shows the extracted values Note that the 3 3 is printed as 3 300 three digits to the right of the decimal point to demonstrate that the code really has interpreted the characters 3 3 as the floating point number 3 3 The second approach to getting configuration parameters reads data directly from the serial port monitor rather than from a file The code in Sketch 21 is shorter than that required to read from a data file on an SD card because there is less checking for hardware status Some Arduino programmers advise against using the String method unless it is absolutely necessary because it is a memory hog It also allows dynamic allocation of array space in memory because the length of a character string doesn t have to be specified in advance This can cause problems with code and memory space Sketch 21 uses the same parseInt and parseFloat methods to extract numerical values as Sketch 20 but it doesn t use St ring methods to extract the text The only 38 disadvantage of this approach is that the number of characters in the file name string must be known ahead of time If the character array dimension for the file name in your code is 13 8 characters for the file name plus a period a three character extension and a null character the file name string you enter must be in the format XXXXXXXX XXX Note that the output doesn t display th
48. e considered equal The operator returns the remainder from integer Table 4 Comparison and division 7 5 equals 2 There is no modulo operator for real _Boolean operators numbers but see the fmod x y math function in Sketch 8 Operation Operator Comparison operators below equal to There are also some bitwise operators which are discussed not equal to l in the language reference linked above There are comparison and less than lt Boolean operators as shown in Table 4 greater than _ _ _ __ _ _ _ gt eek a less than or equal to lt There are many subtleties involved in using arithmetic Greater than or equalto gt comparison and Boolean operators If you are unfamiliar with Boolean operators programming fundamentals at some point you will have problems AND amp amp As just one example remember that x y is NOT the same thing wa l as the assignment statement x y The former statement tests for equality and the latter assigns the current value of y to x Confusing the equality operator with the assignment operator is a common coding error that is very difficult to debug Be careful 2 2 3 Conditional and repetitive execution The standard Arduino IDE installation includes several sketches that demonstrate some of the language features in this section Those examples tend to be more hardware oriented For example the fStatementConditional ino sketc
49. e length of the text string This is because Length is a method of the St ring object which isn t available here Initially the serial port buffer is empty The code while Serial peek lt 0 waits until the user types something into the box at the top of the serial port monitor window and presses the Enter key If the user types 3 9600 3 3 LOGGER10 CSV Enter the code extracts these four values two integers a floating point number and a string If you make a mistake press the reset button on your Arduino board and start over again It is interesting to note that the delay 10 statements in line 15 and 17 turn out to be required for this sketch to work This 10 ms delay is apparently necessary to slow down the code long enough to process data coming from the serial port buffer an important and hard earned lesson about a problem which took a lot of trial and error debugging to find and fix Both Sketch 20 and 21 assume a specific format for the characters saved in a text file or typed into the serial port buffer In both cases the format is integer integer floating point number string separated by commas A space before the numerical values is OK but not between the last comma and the start of the text in Sketch 21 without the String object there is no trim method to strip off white space If the format is changed from what is shown then the code must be changed accordingly The code is not smart enoug
50. e solution but using Figure 16 12 V lead acid battery and 9 V D cell pack with interchangeable two voltage regulators one onboard and another off board is not very efficient As discussed above a more efficient solution is to connect the battery output directly to the input of a switching power supply such as the D24V5F5 discussed above and connect the output of that device to a Vcc pin and ground connectors The connector on the left has a 2 1mm plug The one on the right is to a D24V5F5 regulator with pins for connecting to Vcc and ground on an Arduino board header I made my own connectors for the lead acid battery terminal One pair of wires goes from the terminals to the charger and a second pair goes from the terminals to a connector that can accept whatever business end cable I need For a direct connection from the battery this will be a cable terminating in a D24V5F5 regulator as discussed above I have used the same interchangeable cable arrangement for my 6 pack of D cells with another cable terminating in a 2 1mm plug for the Arduino Uno as shown in 62 Figure 16 because the Uno has a 2 1mm jack and this is the easiest portable solution for testing without any additional connections to the board Although I have noted above that the power demands of an Arduino based datalogger are large relative to similar commercial dataloggers under reasonable sun conditions even in winter those demands can
51. e the reference while a sketch is running analogRead pin number Reads a value from the specified pin For the Arduino s 10 bit analog to digital conversion this is an integer value A between 0 and 1023 and the conversion to voltage is A 1023 REF where 26 REF is the default reference voltage SV or 3 3V depending on the board or the reference voltage set with analogReference The decimal point after 1023 is required because A is an integer value and A 1023 REF will return a value of 0 or 1 if A is 1023 analogWrite pin number dutyCycle The analogwWrite 1 500 second function is not as might be a reasonable assumption the inverse of analogRead Its purpose is to 1 20 5 modify the duty cycle of a pulse ov width modulated PWM square wave see Figure 3 sent to a pin configured to accept this input The frequency of the square wave is about 500 Hz The 10 20 50 allowed values for dutyCycle are w between 0 and 255 For a 50 duty cycle set dutyCycle 128 For a 5 cycle set dutyCycle 13 sv On the Arduino Uno R3 and 18 20 90 similar boards this function works on pins 3 5 6 9 10 and 11 On the Uno Ww R3 these pins are marked with a dash Figure 3 Pulse width modulation See before the pin number On the _ https learn adafruit com downloads pdf adafruit arduino lesson 3 rgb leds pdf Arduino Pro the pins are noted with PWM before the pin
52. efined condition Often knt is incremented or decremented by 1 for each pass through the loop but other values are perfectly reasonable as long as the logic is correct and the termination condition is defined properly with improper increment decrement and termination conditions it is possible to define a loop that will never terminate The increment or decrement operation is done automatically after the last statement inside the loop When the loop is complete knt has a value equal to one increment or decrement step past the last value for which the loop statements were executed Sketch 4 should make this clear It is also clear from this sketch that the loop counter variable named i in this example can be re used in more than one loop in the sketch Finally Sketch 4 shows that it possible to nest loops In this example nested loops are used to calculate values in the rows and columns of a two dimensional table It is possible to use the break command see 2 2 4 Some other program flow control statements to exit from a loop when some condition other than the specified loop terminating condition is met but this kind of conditional control is best implemented with the constructs discussed next 11 while and do while loops These conditional ESR neni Ginn aoe SE Bice com3 BEA execution constructs File Edit Sketch Tools Help allow a block of statements o i en 5 oP DoWwhile 31 A executed repetitively as O rar 4 long as certa
53. elay delay t Month now month Minute now minut Second Don t process th Day now day 07 Second now second dataOutput dataOutput second twice sam Sketch 23 is similar to Sketch 22 but it accepts as input a sampling interval in seconds or minutes and then generates statistics over a longer interval in minutes For example sampling every 10 seconds 44 over a five minute interval generates mean maximum minimum and standard deviation of an input value based on 30 data samples 5 minutes 300 seconds With this code it is not possible to generate statistics over an interval of less than 1 minute It is up to the user to provide input that makes sense Entering 10 s 5 logfilel csv is appropriate but for example 30 m 2 logfilel csv makes absolutely no sense The statistics calculations are of interest The mean of n samples is straightforward X n The standard deviation s of n samples taken from a normally distributed Gaussian population of values the sample standard deviation is X X s amp n 1 For computational purposes the sample standard deviation is calculated by totaling the sum of the X s and the sum of the square of the X s as the X s are read one at a time X2 XP n n 1 The standard deviation calculation can always be performed on any set of values but it assumes a Gaussian distribution of the X values Depending on what is being measured
54. erial print Serial print minPhotocell Serial print Serial print std_devY 4 Serial println endif ECHO TO SERIAL Eaj if ECHO TO FI sprint Er N logfile sh write to file endif ECHO TO FIL maxTemperature Gl 100 minTemperature 150 maxPhotocell 0 minPhotocel1l 1023 7 void setup Serial begin 9600 getConfiguration Serial print sampling interval Serial print dt Serial printin intervalType Serial print logging interval Serial print dtSave Serial printin minutes if intervalType s amp amp dt lt 2 Serial print Configuration error Restart Serial print Log to file Serial println outFile pinMode 10 OUTPUT if ECHO TO SERIAL Serial printin Write to serial port logfile print Y ear logfile print logfile print Month logfile print logfile print Day logfile print logfile print Hour logfile print logfile print PEET logfile print logfile print Second logfile print logfile print day _frac 5 logfile print logfile print meanX 4 logfile print logfile print maxTemperature 4 logfile print logfile print minTemperature 4 logfile print logfile print std_devx 4 logfile
55. f analogRead 0 gt 400 return 1 else return 0 See below for more information about functions A return statement can also be used to exit a function before some code is executed Sometimes this is more convenient than commenting out unwanted code with void myFunction good code here return code you want to be ignored 14 Sketch 7 shows some examples of using these statements FlowControl Arduino 1 0 5 r2 DER Fie Edit Sketch Tools Help FlowControl void setup Serial begin 9600 int i for loop counter float x threshold 100 Serial println using break for i l i lt l00 i x float i i if x gt threshold break else Serial println x Serial println using goto for i l i lt l00 i x float i i if x gt threshold Serial println Emergency exit goto bail else Serial println x bail Serial println using continue for i l i lt 20 i if i gt 3 amp amp i lt 15 continue Serial println i 64 00 61 00 100 00 using goto 1 00 4 00 9 00 16 00 25 00 36 00 49 00 64 00 61 00 100 00 Emergency exit using continue void loop lt Binary sketch size 4 280 bytes of a 32 256 byte maximum Arduino Uno Sketch 7 Some program flow control statements 2 2 5 Serial communication and displaying output As is evident from the sketches shown so far
56. f 30 you must use sin 30 PI 180 It is up to the programmer to use all math functions appropriately for example by not asking for the square root of a negative number It is possible that some of these functions might be computationally intensive enough to cause memory and or performance problems with sketches If so that would favor minimizing the numerical processing done within a datalogger program The only way to find problems is to try your code 17 MathFunctions Arduino 1 0 5 r2 DER Fie Edit Sketch Tools Help PI 3 1415927410 exp x 1 648687212181 logix 0 6931471824 loglO x 0 3010300159 powix y 0 0507657623 sqix 0 25 square ix 0 2500000000 hypot x y 4 3289723396 sqrtix 0 7071067810 cos x 0 8775825500 sin x 0 4794255256 tanix 0 5463025093 acos x 1 0471975803 asin x 0 5235987663 atan x 0 4636476039 atan2Z x 0 1157592177 cosh x 1 1276259422 sinh x 0 5210952758 tanh x 0 4621171474 fuodi x y O 5000000000 fabs x 0 5000000000 floor x 0 0000000000 trunc x 0 00 ceil x 1 0000000000 fnin x O 5000000000 fmax x 4 3000001907 MathFunctions void setup float x 5 y 4 3 Serial begin 9600 Serial print PI Serial println PI 10 Serial print exp x Serial println exp x 10 Serial print log x Serial println log x 10 Serial print loglO x Serial println loglO x 10 Serial print pow x y Serial println pow x y 10
57. g correctly rather than having to remove or comment out all the code that is no longer needed For a stand alone data logger there is probably no reason to waste code writing results to the serial port in addition to writing data to a file on an SD card If you need to make changes and monitor the results just change the value back to 1 again Note that the same results could be obtained by setting a variable name to true or false and then using if statements to include or bypass code But in that case all the code is still included in the compiled sketch and that could waste a lot of memory With the i f endif directive excluded code is simply ignored when the sketch is compiled a potentially important consideration given the Arduino s relatively restricted memory for code 2 2 4 Some other program flow control statements break continue goto label return value The break statement is required for use in the switch construct described above It is also used to exit from loops but some programmers deprecate this practice Most programmers believe that goto statements should never be used because they can result in code that is difficult to debug and maintain Nonetheless both these statements have some legitimate uses if they are used sparingly and appropriately The return statement is typically used to return a value from a user defined function see 2 2 8 User defined functions for example int checkSensor i
58. h to figure out what you mean if you enter something unexpected Note that the code to read configuration values from the serial port Sketch 21 is 5150 bytes while the code in Sketch 20 with the St ring and SD libraries takes almost 18 000 bytes of the 32 256 bytes available for code On a real computer this wouldn t be an issue but sketch size can definitely be an issue for microcontroller programming 39 ReadConfigurationFile Arduino 1 6 3 File Edit Sketch Tools Help ReadConfigurationFile include lt SPI h gt include lt SD h gt include lt String m What s in CONFIG TXT int int float File myFile char c fileName CONFIG TXT int x y float z String outFile void setup Serial begin 9600 Serial println Initializing SD card pinMode 10 OUTPUT if SD begin 10 Serial println initialization failed return Serial println initialization done myFile SD open fileName if myFile x mnyFile parseInt Serial println x y mnyFile parseInt Serial println y z myFile parseFloat Serial println z 3 myFile read skip past the comma while myFile available delay 10 if myFile available gt 0 c myFile read outFile c outFile trim strips off white space Serial println outFile Serial print length Serial println outFile length myFile close else Serial println error opening data file void lo
59. h lower voltage outputs For example the output from a silicon photodiode based pyranometer http www instesre org construction pyranometer pyranometer htm available from the Institute for Earth Science Research and Education has an output of about 250mV in full summer sunlight Under full summer sun the solar radiation reaching Earth s surface is about 1000 W m so for this instrument the logger will provide a resolution of only about 20 W m a resolution too poor for monitoring solar radiation The datalogger shield software uses a 3 3V power source from the Arduino Uno R3 board This improves the resolution a little but not significantly Let s test two subsystems on the datalogger shield the clock and the SD card interface 3 1 1 Real time clock RTC The datalogger shield includes a real time clock an essential component of a system for logging data The coin cell battery will last for several years so it is simply left in place once installed A library is required to use the clock Download the RTClib cpp and RTClib h files at https github com adafruit RTClib Create a folder within the libraries folder libraries RTC1lib and copy both the cpp and h files there You should install libraries only when the Arduino IDE is not running because libraries installed while the IDE is running will not be recognized until the IDE is closed and restarted Communications with the clock are handled through the
60. h uses a potentiometer attached to an analog pin if constructs The if construct allows blocks of code to be executed or not depending on the value of a Boolean expression true or false In compound if statements only the block of statements corresponding to the first true condition if such a condition exists is executed Here and elsewhere in this document code or pseudocode enclosed in square brackets means that that code is optional if Boolean expression else if else Boolean expression For processing more than two conditions you can use as many else if statement blocks as you need For Sketch 3 with x 175 the message x is OK is displayed on the serial port monitor Then the rest of the if construct is ignored That is even though it is also true that x is less than 300 this portion of the construct is never executed for loops for loop constructs are for executing a block of statements a specified number of times These loops are often used to access and process elements of an array see 2 2 7 Arrays below They can also be used to control hardware operations for knt lower or upper limit test knt against upper or lower limit decrement or increment operation on knt Boolean expression 8 IfTest Arduino 1 0 5 r2 File Edit Sketch Tools Help lfTest void setup put your setup code here to run once Serial
61. he beginning of the day on 1 1 1970 not counting any leap seconds that may have been added This may seem like an odd way to keep track of time but because can be very integer calculations done exactly and efficiently this is typical of how programming languages handle time calculations One result is that integers can become too large for the standard 2 byte int data type 432787 or 65 535 for an unsigned integer Note statements such as Serial print now unix time 86400L in which the L forces the 86400 the number of seconds in one day to be treated as a long integer stored in 4 bytes It could also be specified as an unsigned long integer UL or ul See http arduino cc en Reference IntegerConstants for more information about typing integers For specifying long integers lowercase L will work but it is a good idea to use uppercase L rather than lowercase 1 which can too RTC Arduino 1 0 5 r2 Dax Fie Edit Sketch Tools Help include lt Wire h gt include lt RTClib h gt RTC_D51307 rtc void setup Serial begin 9600 Wire begin rtc begin if rtc isrumning Serial println RTC is NOT rumning Set the RTC to the date time this sketch was compiled rtc adjust DateTime DATE_ TIME void loop DateTime now rtc now Serial print now year DEC Serial print Serial print now month DEC Serial print Serial
62. he physical file name 33 9 SDWriteTime2 Arduino 1 0 5 r2 DAR Fie Edit Sketch Tools Help SDWriteTime2 2014 6 25 16 23 15 include lt 5D h gt 2014 6 25 16 23 20 include lt lJire h gt include lt RTClib h gt 20L4 6 25 16i 23225 define ECHO TO FILE 0 2014 6 25 16 23 30 RTC_D51307 RTC Define real time clock object 2014 6 25 16 23 35 File logfile the logging file int t delay_ t 1000 dt 5 log every 5 seconds 2014 6 25 16 23 40 const int chipSelect 10 void setup Serial begin 9600 pinMode 10 OUTPUT if ECHO_TO_FILE Serial print Initializing SD card if SD begin chipSelect Serial println Card failed or not present return else Serial println card initialized char filename J SDWrite csv logfile 5SD open filename FILE_WRITE if logfile Serial println Could not create file return else Serial print Logging to Serial println filename endif ECHO _TO FILE Wire begin RTC begin void loop DateTime now now RTC now t now second if t dt 0 Serial print now year Serial print Serial print now month Serial print Serial print now day Serial print Serial print now hour Serial print Serial print now minute Serial print Serial print t Serial println if ECHO_TO_FILE logfile print now year logfile print logfile print now month logfile print
63. ial write val int bytesSent Serial write str The first method will write a single byte to the serial port val could be an integer or a character that can be represented in one byte e g 57 or Z The second method will write a string of characters to the serial port Optionally you can read the number of bytes written to the port 2 2 6 Math functions The Arduino microcontroller s small size can be deceptive The Arduino language supports many math functions which make it possible to do sophisticated data processing in a datalogger sketch that would otherwise have to be done offline in some other software application As noted previously the Arduino language supports integers and real floating point numbers See the discussion of the real time clock code for more information about signed and unsigned integer constants and variables The Arduino language does not have a separate double floating point number type for higher precision calculations all real numbers are treated as double The language reference home page at http arduino cc en Reference HomePage gives a short and incomplete list of built in if that s the right term math functions However Arduino includes support for the much more extensive set of functions found in the Math h library http www nongnu org avr libc user manual group__avr__math html even though no Math h library folder is shown as part of the Arduino IDE installation In Sketch 8
64. in long i 32 randomSeed 3 51 conditions are met as EA z opposed to for loops in which the number of executions is set ahead of time The statements inside a while loop may not be executed at while Serial peek lt 0 do wait for key press i random 1 100 Serial println i while i lt 50 void loop lt all depending on the initial value of a Boolean expression controlling Binary sketch size maximum execution do while loops are always 1 Arduino Uno on COMS executed at least once because the comparison with the Boolean expression is done at the end of the loop rather than at the beginning It is possible to Sketch 5 Conditional execution loops write loops that will never terminate and it is a programmer s responsibility to make sure this doesn t happen In Sketch 5 the code waits for you to press a key and Enter in the serial port monitor See 2 2 6 Math functions below for more information about using the random number generator The output from this sketch is always the same set of values between 1 and 100 These values are random in the sense that they would pass statistical tests for randomness Note that the loop terminates after seeing a value gt 50 because the test is done at the bottom of the loop switch construct The switch construct controls execution based on matching a value with a list of integer or character values It won t work
65. in outFile pinMode 10 OUTPUT if ECHO TO SERIAL Serial print ADS gain setting Serial printin gain Serial printin Write to serial port Serial println year month day hour minute second day frac A0 Al A2 A3 endif ECHO TO SERIAL if ECHO_TO FILE Serial print Initializing SD card if SD begin chipSelect Serial printin Card failed or not present return else Serial printin card initialized logfile SD open outFile FILE WRITE if logfile Serial println Could not create file return else Serial print Logging to Serial println outFile logfile printin year month day hour minute second day frac light T C endif ECHO TO FILE Wire begin RTC begin adsl115 begin Serial print Gain setting switch gain case 1 ads1115 setGain GAIN ONE DtoA 4 096 32768 Serial println GAIN ONE break case 2 ads1115 setGain GAIN TWO DtoA 2 048 32768 Serial println GAIN TWO break case 3 ads1115 setGain GAIN_TWOTHIRDS DtoA 6 144 32768 Serial println GAIN TWOTHIRDS break case 4 ads1115 setGain GAIN FOUR DtoA 1 024 32768 Serial println GAIN FOUR break case 8 f ads1115 setGain GAIN_ EIGHT DtoA 0 512 32768 Serial println GAIN EIGHT break case 16 ads1115 setGain GAIN_ SIXTEEN DtoA 0 256 32768 Serial println GAIN SIXTEEN break default Serial printlin Oops no such gain setting return
66. int Serial print Day Hour 24 Minute 1440 Second 86400 5 Serial print Serial print photocellReading Serial print Serial print temperatureC 2 Serial printin endif ECHO TO SERIAL if ECHO_TO FILE logfile print Year logfile print logfile print Month logfile print logfile print Day logfile print logfile print Hour logfile print logfile print Minute logfile print logfile print Second logfile print logfile print Day Hour 24 Minute 1440 Second 86400 5 logfile print logfile print photocellReading logfile print logfile print temperatureC 2 logfile printin 36 logfile flush write to file endif ECHO TO FILE delay delay t COM3 Write to serial port year month day hour minute second day frac light T_C Initializing SD card card initialized Logging to TEMPLITE csv 2014 6 26 15 1 55 26 62633 844 27 34 2014 6 26 15 2 0 26 62639 639 27 34 2014 6 26 15 2 5 26 62645 640 27 34 2014 6 26 15 2 10 26 62651 839 27 34 2014 6 26 15 2 15 26 62656 845 27 34 2014 6 26 15 2 20 26 62662 864 27 34 2014 6 26 15 2 25 26 62668 926 27 34 2014 6 26 15 2 30 26 62674 930 27 34 NNN NN NN Figure 7 a Serial port output for MyLightTemperatureLogger ino with ECHO TO FILE and ECHO TO SERIAL both set to 1 a A B E D E E G H l 1
67. lect 4 in the downloaded version must be changed to const int chipSelect 10 to work with 32 this shield In this case the code reports that 6 files including an empty file have already been saved to the card The file creation data and Card type SD1 time are clearly junk This is because a different library SdFat Volume size bytes 128352256 Volume size Kbytes 125344 Volume size Mbytes 122 Initializing 5D card Wiring is correct and a card is present Volume type is FAT16 http code google co m p sdfatlib downloa ds list is required to Files found on the card name date and size in bytes TEST TXT 2000 01 01 01 00 00 36 provide proper time LOGGEROO CS 2000 01 01 01 00 00 0 stamps for files written LOGGEROL CS 2000 01 01 01 00 00 1917 LOGGEROZ CS 2000 01 01 01 00 00 461 to SD cards For these LOGGERO3 CS 2000 01 01 01 00 00 197 purposes this LOGGERO4 CS 2000 01 01 01 00 00 2295 capability does not s omi to be worth the Figure 6 Output from CardInfo ino modified extra code typically logged data should include date and time fields from the real time clock as part of the output saved in the file A reasonable next step is to write date and time data from the real time clock code to an SD card file First recall the real time clock code shown previously in Sketch 17 That code read time and date values from the DateTime object did a couple of calculations
68. loads pdf adafruit 4 Sparkfun Arduino Pro and FTDI USB board channel adc breakouts pdf with Adafruit data logging shield Secs E 00000009000 0009090809 o e282 lt AOD Keadatrui 50 Figure 11 c Back of data logging shield Table 5 Components for a high resolution datalogger Price Component Source as of July 2014 Arduino Pro 5 V microcontroller DEV 10915 14 95 Female header pack PRT 11269 nape ee 1 50 FTDI basic breakout board 5V DEV 09716 14 95 Adafruit data logging Shield PID 1141 19 95 ADS1115 16 bit 4 channel ADC PID 1085 www adatruitcom 14 95 3 position terminal block 2 ED2610 ND www digikey com 0 51 each SD card Various sources 5 7 Total cost approximate not including USB cable and shipping 75 3 2 2 Programming Sketch 24 is a modification of Sketch 22 which uses the hardware shown in Figure 4 The logic for reading and recording data is the same as in Sketch 21 but the code for logging temperature and light data at 10 bit resolution is replaced by code for accessing input from up to four voltage producing sensors connected to the ADS1115 board Configuration parameters for controlling how the data are collected and logged are read from the serial port window when the sketch is uploaded e integer the sampling interval in minutes or seconds e character m or s identifies the sampling interval as minutes or
69. lues elements that can be accessed by name and an index number Index values always start at 0 not 1 The size of an array must be part of its declaration either explicitly or implicitly That is memory space for arrays is allocated statically not dynamically This means that unlike with some languages such as PHP you cannot define additional array elements later in your code You must either declare the size without specifying values or leave the size blank and declare values enclosed in curly brackets from which the code compiler will infer the size Here are some examples of array declarations int IntArray 10 int counters 1 2 3 4 5 float data 3 3 4 5 char greeting 4 hi length of hello will be 14 characters including null char hello J Hello world As noted above the Arduino programming language does not have a separate string data type Strings defined as arrays of characters must contain one more element than the number of characters to allow for a required null character 0 at the end Elements of an array are accessed through an index value which can be an integer constant an integer variable or a calculation that returns an integer result The Arduino language does not check to see if an index value refers to non existent elements beyond the array declaration boundaries Trying to access values outside the defined boundaries will cause problems that can be very difficult to
70. nd from a huge amount of code available online You can find a language reference at http arduino cc en Reference HomePage and there is an Arduino programming style guide at http arduino cc en Reference StyleGuide You can find an Arduino programming tutorial with many examples at http playground arduino cc uploads Main arduino_notebook_v1 1 pdf There are also dozens hundreds of books about Arduino programming 2 2 1 The Minimum Sketch Every Arduino sketch requires both a setup and loop function even if Loop doesn t do anything If the 1oop function is empty there must be something in the setup function or else the sketch won t do anything at all Some of the examples in this section don t do anything inside the Loop function void setup Put your setup code here to run once void loop Put your main code here to run repeatedly 2 2 2 Data type examples type conversions and operators Table 2 shows the data Table 2 Data types and conversions types supported by Arduino Datatype Example Type conversion programming integers and real ane Int a ees INER long long e f g long x numbers characters and Boolean float float x y 333 z 17e 6 float x values See the discussion of the char char cl A char x real time clock code below for boolean only two possible values true or more information about using long false
71. ng the text of all sketches used in this document can be downloaded at www instesre org ProgrammingGuideSketches txt 1 INTRODUCTION 3 2 THE ARDUINO PROJECT DEVELOPMENT ENVIRONMENT 4 2 1 Up and Running 2 2 The Arduino Programming Language 2 2 1 The minimum sketch 2 2 2 Data type examples type conversions and operators 2 2 3 Conditional and repetitive execution 2 2 4 Some other program flow control statements 2 2 5 Serial communication and displaying output 2 2 6 Math functions 2 2 7 Arrays 2 2 8 User defined functions 2 3 Digital and Analog I O 2 3 1 Digital pins 2 3 2 Analog pins 3 AN ARDUINO BASED DATALOGGER 3 1 Using the Adafruit Datalogger Shield to Explore a Datalogger Application 3 1 1 Real time clock RTC 3 1 2 SD card interface 3 1 3 A simple data logging program 3 1 4 Additional software considerations for a data logging application 3 1 5 Putting it all together with complete data logging applications 3 2 A High Resolution Datalogger 3 2 1 Hardware 3 2 2 Programming 4 SOME ADDITIONAL CONSIDERATIONS 4 1 Expanding the Number of High Resolution Channels 4 2 Enclosures for Your Arduino Datalogger 4 3 Powering Your Arduino Datalogger In recent years there has been an explosion of interest in microcontrollers One of the most successful and widely used systems is the Arduino started as a student project in 2005 at the Italian Interaction Design Institute Ivrea Since then that Institute has closed but the Arduino
72. op lt tch uses 17 010 bytes 52 of program storage space Maximum is 2 296 bytes Arduino Uno on COMS I CONFIG TXT Notepad File Edit Format Yiew Help 3 9600 3 3 OUT_FILE CSV 9600 3 300 OUT_FILE C5 Sketch 20 Read values from a text file stored on SD card 40 ReadConfigurationSerial Arduino 1 0 5 r2 DER Fie Edit Sketch Tools Help ReadConfigurationSerial 3 Data entry format int int float OOOO OX 9600 char c outFile 13 8 char file name extensionim 3 300 n 0 index into outFile LOGGERLO CS int x y float z void setup Serial begin 9600 while Serial peek j lt O x Serial parseInt y Serial parseInt z Serial parseFloat Serial read get past comma before string delay 10 Code won t work without this delay while Serial available delay 10 if Serial available gt 0 c Serial read j outFile nJ c nH outFile n 0 Serial println x Serial printin y Serial println z 3 Serial println outFile void loop lt Binary sketch size 5 150 bytes of a 32 256 byte maximum Arduino Uno on COMS Sketch 21 Read values from serial port 3 1 5 Putting it all together with complete data logging applications In this section two sketches will be presented which use the data logging shield with temperature and light sensors Both sketches require that configuration information be s
73. opied into your sketch before it is compiled The if directive is used for including or excluding blocks endif pre compile of statements before a sketch is compiled In this example the pre compile directive is used to turn output to the serial port on or off define ECHO TO SERIAL 1 true if ECHO TO SERIAL Serial printlIn endif ECHO_TO SERIAL 1 echoes data Sek SwitchTest Arduino 1 0 5 r2 File Edit Sketch Tools Help SwitchTest void setup put your setup code here to run once Serial begin 9600 int x 3 switch x case l Serial println x 1 break case 2 Serial println x 2 break case 3 Serial println x 3 break default Serial println x is out of range void loop lt Binary sketch size 1 926 bytes of a 32 256 byte maximun 1 Arduino Uno on COMS Sketch 6 Example of switch construct false 0 doesn t whatever you wish to display You can call the test variable whatever you want ECHO_TO SERIAL was chosen because it describes the purpose of using the directive Pre compile directives are very useful language features when you are writing and debugging code You can turn on printing to the serial port while you are developing your code and then just by changing the value of ECHO_TO SERIAL from 1 to 0 turn it off when everything is workin
74. pins i oO T http arduino cc en Tutorial DigitalPins EEEREN There are three functions available for controlling and accessing digital pins pinMode pin _ number INPUT or OUTPUT Figure 2 Use a digital pin to read the status of a digitalWrite pin number HIGH or pushbutton LOW digitalRead pin_ number Pins configured as OUTPUT the default state can provide a current of up to 40 mA to a connected device enough to power an LED for example but probably not a relay or motor Pins configured as INPUT are used to detect changes in state of a connected devics such as a pushbutton Sketches 13 and 14 show two examples slight modifications of examples from the standard Arduino installation library The Uno R3 has an LED already connected to pin 13 Sketch 13 turns an LED the small yellow LED marked with the red circle in the image on and off with one second in each state This example uses only the hardware already on the board 24 e Blink2 Arduino 1 0 5 r2 DAR Fie Edit Sketch Tools Help Blink2 int led 13 void setup pinMode led OUTPUT void loop digitalWrite led HIGH turn the LED on HIGH is the voltage level delay 1000 wait for a second digitalWJrite led LOW turn the LED off by making the voltage LOW delay 1000 wait for a second Binary sketch size 1 084 bytes of a 32 256 byte maximum 1 Arduino Uno on COM3
75. print now day DEC Serial print Serial print now hour DEC Serial print Serial print now minute DEC Serial print Serial print now second DEC Serial println Serial print since midnight 1 1 1970 Serial print now unixtime Serial print s Serial print now unixtime 86400L Serial println d f calculate a date which is 7 days and 30 seconds into the future DateTime future now unixtime 7 86400L 30 Serial print now 7d 30s Serial print future year DEC Serial print Serial print future month DEC Serial print Serial print future day DEC Serial print Serial print future hour DEC Serial print Serial print future minute DEC Serial print Serial print future second DEC Serial println delay 3000 Binary sketch size 6 040 bytes of a 32 256 byte maximum Arduino Uno on C Sketch 17 Testing the real time clock easily be mistaken for the digit 1 An unsigned long integer can store a value up to 4 294 967 295 3 1 2 SD card interface Like the clock the SD card requires a library Download the SD h and SD cpp files at https github com adafruit SD and copy them into a libraries SD folder Sketch 18 which tests communication with an SD card is taken from the SD library Figure 6 shows output from the CardInfo ino sketch available along with the SD library The code line const int chipSe
76. printed results to the serial port and then waited for 3 seconds delay 3000 before doing it again Sketch 17 does not really produce results every three seconds The delay between getting one set of time and date values and the next is three seconds because of delay 3000 plus the time required to do everything else inside the loop In some applications the additional delay of at least several tens of milliseconds might not even be noticeable but as a result the output from Sketch 17 will periodically skip a second The way to fix this problem assuming that you think it is a problem is to get data from the DateTime object more often and process data only when the seconds returned from DateTime is an integer multiple of the desired interval If t is seconds and dt is the desired sampling interval then data should be processed only when t Zdt is 0 Getting data from the DateTime object more often may put more demands on the processor but you will have more control over the results If dt is at least 2 s then your code should include a delay 1000 to ensure that the same second isn t processed twice Then your logged data will always be at the desired interval relative to clock time If this doesn t matter don t bother In Sketch 18 the 6 line File logfile defines a logical name a handle which is then associated with a physical file name You can use whatever name you like for the handle The logical name is not the same as t
77. r program is doing and often display intermediate values to help with code debugging As will be shown later the serial port monitor window can also be used to provide input data to a sketch while it is running The Serial begin 9600 method opens the port at a communication speed appropriate for the Uno R3 board See 2 2 5 Serial communication and displaying output for information about the Serial object and its methods See 2 3 7 Digital pins for information on digitalWrite You can make changes at any time to a sketch re compile it to make sure you haven t introduced any new errors and then upload the new version It is not necessary to save changes before you upload them Do not save your own sketches in the read only examples folder You can create a new sketch there but the IDE will not save a modification of that sketch in examples You can create another folder for your sketches in the Arduino folder or you can just save them in the Arduino folder 4 Because the ES DEAR Arduino system relies File Edit Sketch Tools Help on software and hardware working LED is on together there are Blink LED is off many opportunities for Blink LED is on turns on an LED on for one second LED is off problems to arise It is a then off for one second repeatedly far beyond the scope of LED is on this document to try to f Pin 13 has an LED connected on most Arduino boards LED is off 7 i Saas LED is on s f give t a nan
78. racters for a file name extension In Sketch 18 the data are written with a csv file extension so they are easy to import directly into a spreadsheet Windows computers don t distinguish between uppercase and lowercase characters in file names saved files will be spelled in all uppercase letters but Linux systems do so be sure to make your spelling of file names consistent as required For writing date and time data to a file my personal preference is to separate year month day hour minute and second by commas rather than writing them in a conventional MM DD YYYY HH MM SS format or DD MM YYYY in European notation This makes it easy to convert a day and time into a fractional day This calculation could be done in your sketch fractionalDay day hour 24 minute 1440 second 86400 where the decimal points force the calculation to use a real number arithmetic otherwise calculations with integer division for example hour 24 would always be 0 Sketch 18 represents the last intermediate step toward writing code to log temperature and light sensor data Yes a pre written logging script for those data is included with the Adafruit datalogger shield sensor package but it has proven much more instructive to approach this application one step at a time by learning how to write code that does just what is needed to test each component of the system and nothing more 3 1 3 A simple data logging program This section uses the
79. rial available delay 10 if c Serial read outFi outFile n 0 void dataOutput delay 10 Serial available gt 0 Define real time clock object Serial read delay 10 Serial read r le n c n photocellReading analogRead photocellPin delay 10 tempReading analogRead tempPin delay 10 temperatureC tempReading aref voltage 1024 0 5y 100 if ECHO TO SERIAL Serial print Year Serial print Serial print Month Serial print Serial print Day Serial print Serial print Hour Serial print Serial print Minute Serial print Serial print Second Serial print Serial print Day Hour 24 Minute 1440 Second 86400 5 Serial print Serial print photocellReading Serial print Serial print temperatureC 2 Serial printin endif ECHO TO SERIAL if ECHO_TO FILE logfile print Year logfile print logfile print Month logfile print logfile print Day logfile print logfile print Hour logfile print logfile print Minute logfile print logfile print Second logfile print 43 logfile print Dayt tHour 24 Minute 1440 Second 86400 5 logfile print logfile print photocellReading logfile print logfile
80. roy the regulator and the board The 9 volts generated from 6 1 5 V batteries connected in series may generate some heat on the board depending on your application but it shouldn t cause problems in long term use A better idea is to supply a regulated 5V for a 5 V board directly to the Vcc pin on the board This bypasses the onboard voltage regulator and will eliminate whatever heat might be generated by the regulator In principle you can use any LM7805 5 V regulator But you should check the output as it is not unusual for some generic LM7805 regulators to produce an output voltage higher or lower than 5V There is still the problem of dissipating heat generated by the regulator but at least this heat is moved off the Arduino board An even better solution is to use a synchronous buck voltage regulator such as the D24V5F5 from Pololu Robotics amp Electronics www pololu com product 2843 These switching or switched mode power supplies are very efficient devices that do not have the heat dissipation problems of linear regulators There is an entire family of these devices for different applications The 5 V D24V5F5 accepts an input of up to 36V and produces an output a 5V at up t aoma Figure 14 D24V5F5 regulator next to more than enough to power an Arduino board project They 4 M7805 regulator in a TO 220 are more expensive than LM7805 regulators 5 instead of package lt 1 but I believe they are worth the e
81. seconds integer a value that sets the gain for the ADS1115 board e string of characters name of the output file 12 total characters including extension Each parameter is separated with a comma with no spaces For example 5 s 1 Logfile1 csv will sample data at 5 second intervals using a GAIN ONE setting see Table 6 below and log the data to logfilel csv ifthe ECHO TO FILE directive is set to 1 The pre compile directives should be changed manually before the code is uploaded to the microcontroller The ECHO TO FILE directive turns data logging to an SD card on 1 or off 0 and the ECHO TO SERIAL directive turns serial port output on or off In both cases there is some initial serial port output to show the configuration parameters and to make sure that the SD card is working properly if ECHO TO FILE is turned on The ECHO TO SERIAL directive is useful for checking the operation of the sketch especially if you make changes to the code The ADS boards have six possible programmable gains with resolutions as shown in Table 6 for both the ADS1015 12 bit and ADS1115 16 bit See ads1115 setGain GAIN ONE in Sketch 24 The total input range applies to differential operation The ADS board returns 16 bit signed integers so the effective digital to analog resolution for single ended operation is only 15 bits For
82. simply a matter of polling these four input channels one at a time and converting the integer value into an analog voltage Figure 11 a shows the connections needed for the ADS1115 board to communicate with the Arduino board Figures 11 b and c show a complete datalogger based on the Sparkfun 5 V Arduino Pro microcontroller Table 5 gives the components list The Arduino Pro board has been chosen over the Arduino Uno R3 because of its lower power consumption The plug in FTDI connector can be switched between multiple devices so only one is needed regardless of how many loggers you build The prices shown are for single units Quantity discounts may be available The Arduino Pro is pin compatible with the Arduino Uno R3 shown in Figure 1 The FTDI board requires a USB mini B connector rather than the Standard B connector on the Uno R3 Standard B connectors are commonly used on printers for example Mini B connectors are used on some digital cameras such as those in the Canon PowerShot series The ADS1115 power 5V and ground connections are visible coming from the upper right hand corner of the of the board The other connections refer to the ADS1115 documentation are made on the back side of the datalogger shield The screw terminals are from right to left in this image GND GND AO A1 A2 A3 x Arduino UNO Figure 11 a Connection the ADS1115 board see Figure 11 b 5 V ADS1115 A D board with https learn adafruit com down
83. source Figure 13 shows voltage as a function of time at room temperature using six Duracell ProCell alkaline D cells in series using a two cell holder and a four cell holder www allelectronics com BH 143 and BH 141 epoxied together and wired in series The initial voltage is a little less than 9V because the batteries were not brand new at the time they were used for this test They are powering an Arduino Uno R3 board and Adafruit data logging shield with an ADS1115 board Data from all four channels are logged to an SD card file every 30 seconds As shown in Figure 13 battery voltage is decreasing by an average of about 0 3V per day indoors at room temperature Possibly the rate of decrease with an Arduino Pro board would be smaller Is this typical performance It is hard to say Battery discharge rate will depend on what you are asking your microcontroller to do and on temperature Outdoors in cold weather battery life will be shortened perhaps considerably So in any critical data logging operation you should check the battery voltage regularly Whenever the voltage reaches 7V the batteries need to be replaced at a lower voltage the microcontroller s on board voltage regulator will stop functioning 60 Is using batteries a 9 0 reasonable solution That is a financial question rather than a technical one In July 2014 a 12 pack of D cell Energizer alkalines cost 10 95 and a 72 pack was 59 70 from www batterymart com
84. sticsFunction Minimum 0 500 void setup const int ARRAY _SIZE 10 int i for loop counter float x ARRAY_SIZE 0 5 77 1 3 8 99 0 53 62 55 81 1 1 GetStats x 10 Serial begin 9600 Serial println Statistics for this array Serial print Mean Serial println A 0 3 Serial print Standard deviation Serial println A 1 3 Serial print Maximum Serial println A 2 3 Serial print Minimum Serial println A 3 3 void loop Arduino Uno on C Sketch 11 Returning multiple values from a user defined function 22 8 CircleStuff Arduino 1 6 3 Ee File Edit Sketch Tools Help r 10 000 circumference 62 83 area 314 16 CircleStuff Sketch 12 CircleStuff xj void GetCircumference float r float c float a c PI 2 r a PI r r void setup float r 10 circumference area Serial begin 9600 Serial print r Serial println r 3 GetCircumference r circumference area Serial print circumference Serial println circumference Serial print area Serial println area void loop program storac Arduino Uno on COMS Sketch 12 Using pointers to return multiple values from a user defined function It is never required to use pointers in Arduino programming but it is sometimes very helpful for getting around the single return restriction of user defined functions as Sketch 12 shows In some situations
85. string Then they keep looking at byes adding them to the string one at a time until they find a byte that represents a character that can t be part of a number Finally they convert that string of characters into an integer or floating point number how they do this last step might be interesting but it is not relevant to this discussion One of the values in the data file accessed by Sketch 20 is the name of an output file to which data will be written If this text string represents a file name it should contain no more than eight characters for the name a period and no more than three characters for the file name extension But the text could be used for anything with no length limitation The Arduino programming language includes a String object that makes it easy to construct a word from a string of bytes in a file In this case the read method is used to skip past the comma after the last numerical value Then characters are extracted one at a time using the read method and they are concatenated to a variable to store the word in this case the output file name See the statement outFile c The while loop to read characters terminates when there are no more characters in the file The delay 10 command may or may not be necessary But see Sketch 20 Finally the trim method is used to strip White space characters spaces and tabs that might exist at the end of the line of characters in the data file or even at th
86. t int to char Serial println char a Serial println d Serial println e void loop put your main code here to run repeatedly lt Binary sketch size 4 194 bytes of a 3 20 maximum Arduino Pro or Pro Mini 6V 16 MHz uw ATmes n COME Sketch 2 Examples of data type conversions and strings The result of a division operation depends on the nature of the numerator and denominator Hence int x 3 y 6 z x y gives a value of 0 but either float x 3 y 6 z x y or int x 3 float y 6 z x y gives a value of 0 5 When you use numerical values in an expression for which you expect a real number division result at least one of those values should include a decimal point in order for values to be treated as real floating point numbers rather than integers For example float z z 2 3 rather than float z z 2 3 The result is 0 even though z is declared as float or float z int x 2 z x 3 rather than float z int x 2 z x 3 the result is 0 Unlike integer arithmetic real number math calculations are not necessarily exact So when determining whether the results of two real number calculations are identical it is a better idea to compare the absolute magnitude of their difference to some arbitrarily small value rather than testing them for equality See if constructs in 2 2 3 Conditional and repetitive execution and Sketch 8 if fabs x y lt 10e 10 then x and y ar
87. t logfile print logfile print minl 5 logfile print logfile print logfile print logfile print mean2 5 logfile print logfile print logfile print logfile print min2 5 logfile print logfile print logfile print logfile print mean3 5 logfile print logfile print logfile print logfile print min3 5 logfile print logfile printin std_dev3 8 logfile flush endif ECHO TO FILE KNT 0 max0 100 min0 100 max1 100 min1 100 max2 100 sumX0 0 sumXX0 0 SumX1 0 sumXX1 0 sumX2 0 sumXX2 0 sumX3 0 SumXX3 0 void setup Serial begin 9600 getConfiguration Serial print sampling interval logging interval Serial print intervalType Serial print Serial print dtSave Serial printin m Serial print statistics computed with Serial print N Serial println samples pinMode 10 OUTPUT if ECHO TO SERIAL Serial print ADS gain setting Serial printin gain 57 max2 5 t std_dev2 8 max3 5 tin std_dev3 8 Month ae Ge Minute max0 5 std_dev0 8 max1 5 std_dev1 8 max2 5 std_dev2 8 max3 5 Serial print dt Serial printin Write to serial port no line breaks allowed in code Put print string all on one line Serial println date time day frac A0 mean AOQ max A0O min A
88. tColor 255 255 0 yellow delay 1000 setColor 80 0 80 purple delay 1000 setColor 0 255 255 aqua delay 1000 1 500 bytes of a 3 0 byte Arduino Pro or Pro Mini 5V 16 MHz uw ATmeg on COME Sketch 16 Using analogWrite on PWM pins to change the apparent color of an RGB LED 29 AN ARDUINO BASED With the programming background presented in Chapter 2 it is now possible to develop datalogger applications For our purposes a datalogger is defined as a device which will operate independently to store analog data in digital form The datalogger application may also include programming to do some internal data processing of input such as averaging multiple values collected over a specified time interval To operate independently the device should include onboard data storage Microcontrollers make it possible to design such standalone devices In general such a project requires four components 1 microcontroller 2 analog to digital ADC converter 3 clock 4 data storage device an SD card 3 1 Using the Adafruit Datalogger Shield to Explore a Datalogger Application The hardware required for a basic standalone datalogger includes an Arduino board and a datalogger shield from Adafruit https www adafruit com product 1141 Boards which piggyback on the Arduino board are called shields The shield includes a real time clock and an SD card interface A datalogger shield packaged with components
89. the board components when an external power supply is used 27 amp Analoginput2 Arduino 1 0 5 r2 SEE File Edit Sketch Tools Help Analoginput2 int sensorPin AQ j ct the input pin int ledPin 13 t LED pin int sensorValue store sensor value 0 1023 void setup pinMode ledPin OUTPUT Serial begin 9600 void loop sensorValue analogRead sensorPin Serial print sensorValue z 0 Serial print sensorValue sensorVa us Serial println analog voltage 0 000 Serial print analog voltage sen sorval ue 0 Serial print sensorValue 1023 5 3 _ ae eke analog voltage 0 000 f turn the LED on sensorValue 2T digitalWrite ledPin HIGH 4 re delay sensorValue analog voltage 0 132 turn the LED off sensorValue 175 digitalWJrite ledPin LOW an al o vo 7 tage 0 855 delay sensorValue g p g i i sensorValue 451 analog voltage 2 204 sensorValue 805 EF ETEEESEIRE analog voltage 3 935 maximum sensorValue 1023 analog voltage 5 000 sensorValue 1023 analog voltage 5 000 Arduino Uno on COMS Sketch 15 Reading voltages on an analog pin Sketch 16 shows how to use analogWrite This sketch has absolutely nothing to do with data logging but it does show an interesting use of the PWM pins varying the effective voltage to the three LEDs in a three component RGB LED It uses three 1K current limiting resistors and a
90. the clock is 0 at the start of each hour Some output from 2 logging interval 2s second sampling written to the 109 to file XXXXXXXX XXX Write to serial port serial port is shown in Figure year month day hour minute second day frac light T C 9 with some data sampled in 2014 7 23 15 18 40 23 63796 715 28 31 my office at 2 minute 2014 7 23 15 18 42 23 63799 680 27 99 intervals written to a cav 20L4 7 23 15 18 44 23 63801 677 27 99 gt 2014 7 23 15 18 46 23 63803 680 28 31 file and opened in Excel The 2014 7 23 15 18 48 23 63806 681 27 99 temperature graph is a good 2014 7 23 15 18 50 23 63808 684 27 34 E 2014 7 23 15 18 52 23 63810 851 28 96 ilustration of the limitations OF 201177723 15 18 54 23 63813 913 28 63 the Arduino s built in 10 bit 2014 7 23 15 18 56 23 63815 915 28 63 A D resolution the 2014 7 23 15 18 58 23 63817 927 28 31 temperature resolution is about Figure 8 Sample output from Sketch 22 0 3 C Although this may seem coarse typical accuracy for the TMP36 sensor is only 1 C around room temperature and 2 C over its 40 C to 125 C range 1000 32 0 http www analog com en mems 900 31 5 sensors digital temperature 800 31 0 sensors tmp36 products product ht 5 700 30 5 Y ml The appropriateness of f 600 300 analog to digital conversions must 500 i 29 5 z as always be judged based on the 3 400 ee Hea
91. uage Arduino boards are deceptively small the Uno R3 board is about the size of a credit card but they have many of the computational capabilities of a real computer The size of the allowed code is smaller than allowed on a bigger computer but within that constraint the possibilities are extensive The major difference between microcontroller programming and conventional programming for scientific and engineering computation for example is that the essential purpose of microcontroller programming is to control hardware In this document the hardware of interest is restricted mostly just to those devices needed to construct a datalogger This section will mostly ignore hardware interfaces in 5 favor of focusing on some programming fundamentals as they are implemented with Arduino A few language syntax elements are shown in Table 1 Many more syntax elements are discussed in the following sections Arduino uses a C C based Table 1 Some language syntax language Its syntax and structure are Language syntax symbol or word Required to terminate code statement Multiple semicolon terminated statements can appear on the same line similar to other languages such as JavaScript and PHP Anyone with experience programming in any of these Define a block of code languages should have no difficulty Define a one line comment programming for Arduino although f Ki Define multi line comments
92. upplied as input in the serial port window Based on the discussion in 3 3 this seems like the easiest way to get configuration parameters Both sketches include pre compile directives ECHO TO FILE and ECHO TO SERIAL that direct output to an SD card or to the serial port In either case there is some output to the serial port that displays the selected configuration parameters before reading values The pre compile directive values must be set manually with 1 for on or 0 for off before the sketch is uploaded Sketch 22 logs values at prescribed intervals expressed in seconds or minutes typing 5 m logfilel csv in the serial port window records values every 5 minutes and writes them in logfilel csv assuming that the ECHO TO FILE directive is set to 1 if it is set to 0 and ECHO _TO SERIAL is set to 1 then the values are displayed in the serial port window and the file name is ignored 41 The data collection always starts at a time that is an even multiple of the sampling interval That is for 2 second sampling the samples are recorded at 0 2 4 seconds but not at 1 3 5 seconds For the example shown in Figure 8 the sampling started at 40 seconds but it wouldn t have started at 39 seconds For this code the shortest sampling interval is 2 seconds and the longest is one hour For one hour sampling enter Oe My cs because the minutes value returned by
93. ussion of Sketch 23 The gain was set to 16 Even though the voltages are small compared to the total range of 0 256V the 16 bit resolution is still more than adequate for this measurement For a calibrated pyranometer which this one wasn t the sketch could be modified to convert voltage to watts m 0 012 10s sampling 5 min logging 30 samples 0 010 g o g 2 Pyranometer output V 0 000 24 34 24 36 24 38 24 40 24 42 24 44 24 46 24 48 24 50 Fractional days August 2014 Figure 12 Sample statistics output from one channel of an ADS1115 board 59 4 SOME ADDITIONAL CONSIDERATIONS 4 1 Expanding the Number of High Resolution Channels Because up to four different addresses can be assigned to each ADS1015 or 1115 board up to four boards can be connected at the same time For details see https learn adafruit com adafruit 4 channel adc breakouts Whether this is worthwhile or not depends on your application Calculating statistics for each channel produces 4 values per channel mean maximum minimum and standard deviation a total of 16 values for one board This is potentially a Jot of data to handle On the plus side you can assign different gain settings to each board 4 2 Enclosures for Your Arduino Datalogger Finding an appropriate enclosure for an Arduino project is not just a matter of appearances Using any kind of exposed electronics in very humid environments or in
94. void loop DateTime now RTC now Year now year Month now month Day now day Hour now hour Minute now minute Second now second if intervalType s if Second dt 0 dataOutput if intervalType m if Minute Sdt 0 amp amp Second 0 dataOutput delay delay t 54 Sketch 25 is a modification of Sketch 23 It calculates statistics for each of the four input channels For example 10 s 5 logfilel csv samples data every 10 seconds and calculates statistics with thirty values over 5 minutes Statistics cannot be calculated over an interval of less than 1 minute The longest time period for calculating statistics is one hour specify O minutes in the configuration input Sketch 25 HiResDataLoggerB Format for serial port window input int dt char m or s int dtSave 1 60 int gain 1 2 3 for 2 3 4 8 or 16 string xxxxxxxx xXxXx include lt SD h gt include lt Wire h gt include lt RTClib h gt include lt Adafruit_ADS1015 h gt define ECHO TO FILE 0 define ECHO TO SERIAL 1 Adafruit_ADS1115 ads1115 float max0 100 min0 100 max1 100 min1 100 float max2 100 min2 100 max3 100 min3 100 float sumxX0 0 sumXX0 0 sumX1 0 sumXX1 0 sumX2 0 sumXX2 0 sumX3 0 SumXX3 0 float std_dev0 mean0 std_devl meanl std_ dev2 mean2 std_dev3 mean3 a D float float adc0O adcl adc
95. widely used Inter Integrated Circuit I2C protocol which allows various devices to communicate with each other http tronixstuff com 2010 10 20 tutorial arduino and the i2c bus That communication is managed by the Wire h library which is part of the standard Arduino installation Sketch 17 shows how to use the real time clock and its library The 6 COM3 rtc now method provides access to year month day hours minutes and 2014 7 14 ll 2 1 seconds Some output from that sketch is since midnight 1 1 1970 140533572ls 16265d shown in Figure 5 The first time you run now 7d 30s 2014 7 21 11 2 31 Sketch 17 with a new clock the output 2914 7 14 11 2 4 since midnight 1 1 1970 14053357248 16265d 7 22 Remove the line comment from line DOM T TET ee ge eee ee 2014 7 14 11 2 7 10 and reload the script This will set the since midnight 1 1 1970 14053357278 162654 clock according to your computer clock now 7d 30s 2014 7 21 11 2 37 After this has been done once you will not agree with your computer clock shouldn t have to do it again for years Ewe OU nen BRERA as long as you don t remove the battery In the sample code from Adafruit the Serial begin 57600 statement must be changed to Serial begin 9600 The sampling interval is every 3 seconds 3000 milliseconds see delay 3000 at the end of the loop 31 Time calculations are based on the number of seconds since t
96. with real numbers The case values in the list don t have to be in any particular order This construct is often more clear than using a lengthy if else if statement However unlike if statements which execute only the first true branch each case in the switch construct requires a break statement to exit when the first match with int value or variable is found without a break all the other remaining operations will also be executed The default keyword provides the opportunity for responding to not finding a match Often this response might be to display a message explaining that no match was found 12 toMatch case choicel switch Do something when toMatch equals cboicel break case choice 2 Do something when toMatch equals choice2 break additional cases default Do something if toMatch doesn t match any available choice The switch construct cannot be used for matching with real numbers Use an if construct instead In Sketch 6 the message x 3 is displayed in the serial port monitor If the value of x is changed to 4 the message x is out of range will displayed Pre compile directives for conditional execution Table 1 gave two examples of pre compile include and define These directives can be used to alter what the Arduino directives IDE sees when it compiles your sketch For example the include directive results in having the specified h file literally c
97. xtra expense http www pololu com product 2843 61 The D24V5F5 board shown in Figure 14 next to the most common packaging for an LM7805 regulator is 0 5 tall The pins holes on the board have standard 0 1 spacing so they can be connected directly to Vcc and a ground pin on an Arduino board header For long term continuous outdoor operation solar power is a reasonable approach but you need to be careful Typically such a system consists of a 12 V lead acid battery and a solar battery charger One solution from www batterymart com is shown in Figure 15 This battery charger maintainer has suction cups so you can mount it on the inside of a window as well as using it outdoors The battery is 100 cm 4 long The solar panel is 240 cm 9 5 square www batterymart com p 12v 1_3ah sealed lead acid battery html SLA 12V1 3 12 95 July 2014 www batterymart com p blsolar 2 12v 2_5w solar panel html ACC BLSOLAR2 24 95 July 2014 Figure 15 Components for a solar charged battery supply A lead acid battery can be charged to well over 12V and as noted above this can cause problems My advice Do not connect a lead acid battery directly to the power input on an Arduino board To protect the onboard voltage regulator you could connect an LM7809 9 V regulator between the battery and the board s power input which will keep the heat generation to an acceptably low ea level on the board This is a reasonabl
98. y hour minute second day frac light T C endif ECHO TO SERIAL if ECHO TO FILE Serial print Initializing SD card if SD begin chipSelect Serial printin Card failed or not present return else Serial printin card initialized char filename TEMPLITE CSV logfile SD open filename FILE WRITE if logfile Serial println Could not create file return else Serial print Logging to Serial println filename logfile printin year month day hour minute second day frac light T C endif ECHO TO FILE Wire begin RTC begin analogReference EXTERNAL void loop DateTime now RTC now Year now year Month now month Day now day Hour now hour Minute now minute Second now second if Second dt 0 photocellReading analogRead photocellPin delay 10 tempReading analogRead tempPin delay 10 temperatureC tempReading aref voltage 1024 0 5 100 if ECHO TO SERIAL Serial print Year Serial print Serial print Month Serial print Serial print Day Serial print Serial print Hour Serial print Serial print Minute Serial print Serial print Second Serial pr

Download Pdf Manuals

image

Related Search

Related Contents

Omron Healthcare GOSMART HJA-301 User's Manual    NOREMARK User`s Manual - Warner College of Natural Resources  20" PURE FLAT STEREO COLOUR TELEVISION  SikaWrap 300C - Sika Colombia  U300/UD300 Series Ice Dispensers  intext:Bedienungsanleitung filetype:pdf  Whirlpool LGQ8000JQ1 User's Manual  MULTI-PURPOSE THERMAL IMAGER IRI 4010  Installation Manual for rainbow DSP 1.8  

Copyright © All rights reserved.
Failed to retrieve file