Recommended Reading Material:Table of Contents<< Instructor's Page
<< Student's Page Recommended Reading Material: Instructions: Digital I/O Operations: Digital I/O operations (Interrupt Driven): Analog Input Additional Notes & troubleshooting: 1. http://iotdk.intel.com/docs/master/mraa/python/ 2. https://github.com/intel-iot-devkit/mraa/tree/master/examples/python 3. Grove Temp 1.2 Wiki: http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor_V1.2 4. Sensor Interface Examples (not using mraa): https://github.com/intel-iot-devkit/upm/tree/master/examples/python Instructions: Working with the Intel Edison as an embedded platform implies that we will encounter multiple situations where sensor interfacing is a must. This particular page takes a look at working with the digital & analog interfaces to the Intel Edison. To do so, we will be using the mraa library which is a C/C++ library for low level communication to the I/O pins on a number of boards, including the Intel Edison. This library has a number of bindings to python, Java, Javascript. This tutorial focuses on the python interface. While this tutorial tries to be self-contained, for new users of Python, do explore this link as a basic but comprehensive review of Python. If you haven't already, please install the mraa library following the instructions on the installation page. Hardware requirements: Grover Sensor Kit (or basic interfaces to the GPIOs of the Arduino board), Intel Edison and corresponding accessories. For the purpose of uniformity and so as to serve as a quick reference for future programs, we begin by creating a directory in which we shall store all these programs. mkdir ~/Lab1 cd ~/Lab1 mkdir Mraa_exercises cd Mraa_exercises Digital I/O Operations: 1. Connect an LED or buzzer to the D6 port & the switch to the D8. 2. Open a new file (using vi or nano): nano DIO.py Inside the file add the following lines of code. Explore the comments in case of doubts. import mraa import time switch_pin_number=8 buzz_pin_number=6 # Configuring the switch and buzzer as GPIO interfaces switch = mraa.Gpio(switch_pin_number) buzz = mraa.Gpio(buzz_pin_number) # Configuring the switch and buzzer as input & output respectively switch.dir(mraa.DIR_IN) buzz.dir(mraa.DIR_OUT) print "Press Ctrl+C to escape..." try: while (1): if (switch.read()): # check if switch pressed buzz.write(1) # switch on the buzzer time.sleep(0.2) # puts system to sleep for 0.2sec before switching buzz.write(0) # switch off buzzer except KeyboardInterrupt: exit 3. Save and exit the file (Ctrl + X in nano & Esc-followed by ":wq" in vi editor) 4. To test the above file, python DIO.py Digital I/O operations (Interrupt Driven): 1. With your connections as above, create a new file by the name of "ISR.py" 2. Add the following lines of code to this ISR.py file. Save and exit as before. import mraa import time switch_pin_number=8 buzz_pin_number=6 # We define the Interrupt Function here. # This function is executed everytime an interrupt is triggered. def interr_test(args): buzz.write(1) time.sleep(0.2) buzz.write(0) switch = mraa.Gpio(switch_pin_number) buzz = mraa.Gpio(buzz_pin_number) # Configuring the switch to input & buzzer to output respectively switch.dir(mraa.DIR_IN) buzz.dir(mraa.DIR_OUT) #The command below enables the interrupt. switch.isr(mraa.EDGE_RISING, interr_test, interr_test) # The interrupt is going to be valid for as long as the program runs # Therefore we setup a dummy "do-nothing" condition try: while(1): pass #"do-nothing" condition except KeyboardInterrupt: buzz.write(0) exit 3. The code above accomplishes the same task as the method in the DIO (non-interrupt) section. Compare the codes, review the similarities & observe the differences over the two files. Analog Input 1. For this section, connect the Temperature sensor to the A1 channel. 2. Reading data from the analog input channels is fairly straightforward, as above. 3. For example, if you create a file name AnalogInput.py with code as follows: #!/usr/bin/env python import mraa try: tempSensor = mraa.Aio(1) print (tempSensor.read()), "is the current temperature" except KeyboardInterrupt: exit 4. You will see arbitrary values, that do not resemble temperature values. 5. These are values on the n-bit ADC scale. If the system has a 2-bit ADC, for example, the system has 4 unique states (from 0 to 22-1: 00, 01, 10, 11) For an n-bit ADC therefore you have values from (0 to 2n-1). This YouTube link may help clarify that further. Here we have a full range scale of 0 to 1023. 6. For the subsequent calculations, we are going to be using data sourced from the Grove Temperature Sensor wiki page. 7. Further from the resources section on the above wiki page, we get the schematic for our temperature sensor which we shall use. 8. With Vcc corresponding to the full scale value of 1023, we use the raw data above as the value for v and calculate the value of r. 9. Following this we use, http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor_V1.2#Reference 10. Subsequently calculating Temperature in Celsius is left as an exercise. Additional Notes & troubleshooting:1. The temperatures are all in Kelvin. 2. The wiki link includes sample C code that you may want to use as reference.
0 Comments
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |