Workshop III: Analog and Sensors Last Updated October 30, 2013
Table of Contents The Analog to Digital Converter (ADC)... 3 Figure 1: Analog comparator... 3 Resolution... 3 Figure 2: Analog voltage vs. analog conversion... 4 ADC Reference... 4 Differential vs. Single Ended... 4 Signed vs Unsigned... 5 Sensors... 5 CDS Cell (Light Sensor)... 5 Figure 3: CDS cell... 5 Sharp Infrared Range Finders... 6 Figure 4: Sharp IR (GP2Y0A41SK0F)... 6 Figure 5: Sharp IR voltage vs. distance... 7 Module I: Light Sensor... 8 Procedure... 8 Hints... 8 Module II: IR Range Finder... 9 Figure 6: Sharp IR data and equation... 9 Procedure... 9 Hints... 9 Module III: Poor Man s Serial Terminal Oscilloscope... 10 Procedure... 10 Hints... 10
The Analog to Digital Converter (ADC) An analog to digital converter or (ADC) is a device that equates analog voltages to digital (numeric) values. The basis of the analog to digital converter is the concept of an analog comparator displayed in Figure 1. IN+ IN- OUT Figure 1: Analog comparator A comparator is a device with two inputs, IN+ and IN-, and one output OUT. When the value of IN+ exceeds IN-, OUT signals HIGH. When IN- is greater the IN+, OUT signals LOW. This is an example of a 1-bit resolution ADC. Resolution ADC resolution is rated in bits. A 4-bit ADC would have a value range of 0-16 because 2^4=16. A 10- bit ADC would have a value range of 0-1024. Figure 2 Displays the relation to an analog voltage and a 4- bit (5V) ADC. The ADC values 0-16 have been scaled to the same magnitude of the analog voltage.
Volts 5 4.5 4 3.5 3 2.5 2 1.5 1 0.5 0 Analog Digital 0 1 2 3 4 5 6 Radians Figure 2: Analog voltage vs. analog conversion Notice the ADC conversion approximates the analog sine wave using 16 steps. The better the resolution the better the tracking will be. ADC Reference One of the more important concepts about ADCs is their use of a reference voltage. When an analog signal is passed to an ADC, a fixed analog reference is used to create a digital conversion value. Reference selection dictates the range and effective resolution of the ADC. If a project required the reading of ~100mV signals, a 5V ADC reference voltage would not be practical. The reason being only 1/50 th of the ADC s conversion range would be utilized. Differential vs. Single Ended Voltage is a differential measurement. By definition voltage is the difference of electrical potential. When a single connection of a circuit is said to have a voltage of X, this infers a circuit Gnd reference (0V). This is referred to as a single ended voltage measurement. ADCs that support single ended measurements require a single signal connection, since Gnd is already common. Differential ADCs, or ADCs whom support differential measurements, require two signal connections, positive and negative. Differential ADC conversions have no correlation to circuit Gnd. There are many times that a differential measurement is preferable to a single ended measurement. Such an example would be a Wheatstone bridge circuit.
Signed vs Unsigned ADCs that support both positive and negative voltage readings are signed. This means that the most significant bit of resolution is reserved to designate a negative or positive voltage reading. Sensors CDS Cell (Light Sensor) A CDS Cell (Figure 3)is an ambient light sensor. These sensors have a variable resistance that is affected by light intensity. The particular sensors we will be using range from ~500k ohms (DARK) to ~25k ohms (LIGHT). These sensors are commonly used for camera exposure, camera shutter control, as well as smart night lights etc. Figure 3: CDS cell
Sharp Infrared Range Finders Sharp infrared range finders, or Sharp IRs for short, are very popular sensors in the hobby robotics world. These sensors report an analog voltage correlating to a distance measurement. The Sharp IR we will be interfacing with is displayed in Figure 4. The corresponding distance vs. voltage chart is displayed in Figure 5. Figure 4: Sharp IR (GP2Y0A41SK0F)
Figure 5: Sharp IR voltage vs. distance
Module I: Light Sensor In this module you will write a program to read an analog voltage. The program will also use serial communication to print these values to the screen Procedure 1. Delete the contents of your tortoise svn repository 2. Use Tortoise SVN to grab the current image of the repository 3. Open Atmel Studio a. Open the solution Epiphany-DAQ from your repository. i. The same solution as all previous workshops b. Locate the Solution explorer window within Atmel Studio. i. Open folder workshop 3 1. Open the file light.c. 4. Create a program in light.c that will a. Read an analog conversion from DAQ channel 0. b. Report the analog conversion to your screen at a frequency of 10Hz i. Format: ADC CHn = value <repeat on a new line each time> 1. n represents the channel number 2. value represents the conversion value.. 5. Compile your program 6. Upload your program using the Chip45boot2 GUI. 7. If your program is functioning properly continue. Otherwise go back to step 4. 8. Augment light.c to now print a voltage instead of the analog conversion value a. Format: ADC CHn = value <repeat on a new line each time> b. Value will be the voltage c. Bonus points for formatting or only 3 decimal places. Hints Use RTC functions to control the refresh rate Don t forget to initialize all or your peripherals o This includes calling sei() to enable interrupts. Consult the workshop II manual for help using the function fprintf When printing a voltage the number will no longer be an integer In C if you divide a value by an integer a integer result is returned o You must divide by a number containing a decimal point to have a floating point value returned. The DAQ ADC is signed, has 14-bit resolution, and uses a 5V reference
Module II: IR Range Finder In this module you will be printing distance measurements based off of Sharp IR data. Use the equation form Figure 8 in your code to calculate distance. 3.5 3 2.5 Volts 2 1.5 1 Sharp IR Data Power (Sharp IR Data) 0.5 0 0 10 20 30 40 Distance (cm) y = 10.594x -0.93 Figure 6: Sharp IR data and equation Procedure 1. Remove but do not delete light.c from your solution in Atmel studio. 2. Import IR.c into the solution. 3. Write a program that will print with the format a. IR Dist = disatnace cm. i. Distance will be the number of centimeters 1. Formatting for 2 integer and 2 decimal places is preferred. 4. Once you are finished with your program compile and upload it to the board. 5. Test your sensor reading with a ruler if one is available. Hints In order to use an exponent in C you must use the pow function. o Format: pow(base,exponent); The DAQ ADC is signed, has 14-bit resolution, and uses a 5V reference
Module III: Poor Man s Serial Terminal Oscilloscope In this module you create a program that plots analog data in a serial terminal Procedure 1. Remove but do not delete IR.c from your solution in Atmel studio. 2. Import serialscope.c into the solution. 3. Write a program that will will read a voltage on adc channel 0. a. Write a for loop routine to print up to 39 spaces before writing the character o b. The goal is to determine how many spaces to print based off the voltage read. c. FYI 5/39 = ~.128V. d. Use a refresh rate of 20Hz 4. Once finished compile and program. 5. If everything works try adjusting the refresh rate to be higher and higher Hints For loop syntax o For(variable assignment ;loop condition; increment){} o Example: for(i=0,i<40;i++){} The DAQ ADC is signed, has 14-bit resolution, and uses a 5V reference 115200 baud is advised.