• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.


Lab 4 2010

Page history last edited by Wendy Ju 9 years, 6 months ago

Make a data logger



Let's get some hands-on experience with the continuous input values provided by analog sensors: potentiometers, accelerometers, distance rangers, and the like. We'll then record the data to the EEPROM of the Teensy and play it back on your LCD display and serial monitor.



Work your way through Parts A, B, C (optional), and D. Send lab reports to ee47-spr0910-staff@lists.stanford.edu!


In The Report

Include your responses to the green questions. Include snippets of code that explain what you did, and please follow the Lab Report Guidelines. Deliverables are due one week after your lab session. 


Part A. Resistance Varying Sensors

In Lab 2, you used a potentiometer to control the brightness of LEDs. Now let's do that again, but with the Arduino in the mix.


First, it's important for you to understand that analog input ("analog pin 0") on your Teensy board shares pins with digital input, but it references different pin numbers. These numbers are indicated on the Teensy pinout page (but not on the board itself!) as A0–A10 or ADC0–ADC10, depending on which diagram you use. Note that you should not include the prefix "A" or "ADC" in your command—just the number.


So while a digitalRead or digitalWrite command reads or sends only a logic-level high or low, an analogRead or analogWrite command reads or sends a range of values. Nice!


1. Potentiometer

Set up the LED output and potentiometer input circuits from the following schematic on your breadboard. Don't forget that there's a big difference between "analog pin 0" and "pin 0."

[Peek at the breadboard photos.]


Now, try the code in File->Examples->Analog->AnalogInput.  This example code is for a different kind of Arduino, so you will need to edit the code to output to the pin we want.


a. What line(s) of code do you need to change?

b. How would you make the LED on the Teensy light up instead


Drawing on your previous Lab's work, change the code so that the LED fades and brightens with the analog value of the potentiometer. (Save this code! We'll be using it again soon...)



2.  Writing to the Serial Monitor

By now you've probably wished more than a few times that you could ask the Arduino what is up when things don't work the way you think they should! The LCD display from the last lab is a great and helpful tool for debug purposes; the serial monitor is another. 


Use the code from File->Examples->Communication->Graph as a template to print data from your potentiometer to the serial monitor. Don't disconnect the USB cable after uploading the code; instead, use the serial monitor button on the Arduino software interface (the one that looks like ) to see the data coming from the Teensy.


a. Based on the readings from the serial monitor, what is the range of the analog values being read?

b. How many bits of resolution does the analog to digital converter (ADC) on the Atmega32U4 have?


You can also read inputs from the serial monitor, or wait for the serial monitor to open before spewing data over the USB line! A nice tutorial on the basic serial transmit functions can be found at http://www.pjrc.com/teensy/td_serial.html. You can see from the sample code included in the comments of the Graph program that you could use the serial communication functions to communicate data from your sensors to other programs, such as Processing, Flash, MaxMSP.


For this lab, you can use the serial monitor and/or the LCD whenever you are told to display something, depending on what you think is easier/prettier.


3. Flex Sensor

The potentiometer is an instance of a voltage divider circuit, which we discussed in class. As you might recall:

The Flex sensor changes resistance between 10k Ohms (straight) and 60k-110k Ohms (bent). The datasheet is here. We'll build a voltage divider circuit with a 22k resistor (red-red-orange-gold), using the Flex sensor with the fading LED/potentiometer Arduino code from the last exercise:

[Peek at photos of the breadboard.]


a. What kind of voltages should we expect? 

b. How does the range of the LED's brightness change compared to the potentiometer?


Incorporate the LCD or serial monitor into your fading LED/potentiometer code so that you can read out the exact analog value that you are reading in on Analog Pin 0. It's your own lowly multimeter! Change the LED fading code values so that you get the full range of facing from your Flex sensor.


c. Include a copy of your Lowly Multimeter code in your lab write-up.


4. Force Sensitive Resistor

Just like the Flex sensor, the FSR changes resistance - in this case, when pressure is applied to the FSR (Wikipedia page). We can reuse the same circuit as before. (It's okay to remove the LED, though.)


Build two FSR circuits to enable a game of thumb wrestling. Use the LCD or serial monitor to indicate who is squeezing their FSR harder!


a. Include a copy of your FSR thumb wrestling code in your lab write-up.


Part B. Voltage Varying Sensors 

Some more sophisticated sensors have ICs that measure physical phenomena and then output an analog voltage level, varying voltage much as a voltage divider circuit would.


1. IR Distance Sensor

The circuit for the distance sensor is simple: just connect red to 5V, black to ground, and yellow to analog input 0.

(More Info on Sharp Distance Sensors).


Use your Lowly Multimeter program to a look at the data the sensor returns. What happens with the field of view is clear? Move your hand over sensor and see how the readings vary with distance.


a. Describe the voltage change over sensing range of the sensor.


2. Accelerometer


The accelerometer in your kit is a 3-axis, selectable range accelerometer (Tom Igoe of NYU as a super page on this accelerometer here). The acceleration in the x,y and z directions is reported out as an analog voltage


The pins of the accelerometer are as follows:


  1. Vcc - Voltage, 3.3V
  2. GND - Ground
  3. X - X axis output, 0 - 3.3V
  4. Y - Y axis output, 0 - 3.3V
  5. Z - Z axis output, 0 - 3.3V
  6. GS1 - G-select 1
  7. GS2 - G-select 2
  8. SLP - Sleep


The connections you need to make are VDD to 5V, GND to ground, and X, Y, and Z to analog pins 1, 2, & 3 on the Arduino board. The G-select pins should be grounded (to set the G-range to 1.5g) and the sleep pin should be set high.


The File->Examples->Sensors->ADXL3xx code is meant to read values from a 3-axis accelerometer out to a computer over the Arduino serial monitor. Use your mighty coding skills to indicate what your readings are on the X, Y and Z axes of the accelerometer on your LCD panel!


Now, get a feel for the data the accelerometer provides. Use the Light Dimmer presets, which will track analog values on A0 and A1. Then pick up the Arduino+accelerometer board and tilt it in various directions. Start by holding it so that the accelerometer board is parallel to the ground. Find in which direction the X reading increases and decreases; do the same for the Y reading. Are the labels on the accelerometer board correct?


a. Include your accelerometer read-out code in your write-up.


Part C. (Optional) Count/Time-Based Sensors

One last type of sensor!


1. Rotary Encoder

Like a potentiometer, a rotary encoder has 3 pins; unlike a potentiometer, an encoder can be spun round and round without stop. Rotary encoders use quadrature to tell how fast and in which direction you are turning a knob. 


The circuit below is the "correct" way of building a rotary encoder with your Arduino, but it has a lot of "nice" features that aren't strictly necessary for functionality.



To actually hook up your encoder, just use the 3-pin side. Hook the middle to ground, and the "A" and "B" pins to digital pins 14 and 15 of your Teensy.


What is going on in this circuit? The Phase A and Phase B pins actually behave like switches, so the pins have pull-ups so that they will be high by default, until they are pulled low by the encoder (your Teensy actually uses its own internal pull-ups). The resistor and capacitor combo also forms a low-pass circuit to eliminate stray voltage spikes that might occur from the quick switching (this is called "debouncing").


Use the following rotary encoder code to see if you have hooked the encoder up correctly!


Part D. Logging values to the EEPROM and reading them back



1. Design your logger

Think about some thing that could be logged using the sensors you've learned to use! It's okay if the application is somewhat fictional; it's just nice to have a story in mind that drives your design.


Your data logger will have two main modes: one where it logs data and another where it plays the data back. Think a little about what sensors you would like to log data from and how you would like to display your data. Create a state diagram sketch that indicates how you'd like to switch between one mode and the other, and also what you'd like the program to do in each state. This can help you decide what buttons or knobs might be useful for your design.


You might make changes to your design before this lab is complete.


a. Turn in a copy of your final state diagram.


2. Reading and writing values to the EEPROM

You might remember from our Microcontroller Architecture lecture that the Atmega32U4 on the Teensy has 1024 bytes of internal EEPROM (Wikipedia: what is EEPROM?).


a. How many byte-sized data samples can you store on the Teensy?

b. How would you get your analog data from the ADC to be byte-sized? 


Use the code in the File->Examples->EEPROM as a template to write and read your own analog values to the Teensy's EEPROM. (Ignore what they say about the EEPROM having only 512 bytes. You'll have to adjust your code to match the byte size of the Teensy.)


3. Create your data logger!

Now it's up to you to integrate the software and hardware necessary to interface with your data logger! Your logger should be able to record a stream of analog data (at a sample rate of your desire) and then play it back at some later point in time. You are welcome to play back to either the 16x2 LCD or the serial monitor. Because the EEPROM is non-volatile, you should be able to play back the data long after you initially recorded it!


a. Use the lab camera to record and upload a short demo of your logger in action.

Comments (0)

You don't have permission to comment on this page.