Reading from analog components

Digital components can fit in Boolean data type. Floating point data type is correct to describe analog values. BoneScript provides the function analogRead() to read the voltage at the analog pin. These pins are input-only. So, there is no need to initialize them using pinMode(). Here is the prototype of the analogRead() function:

analogRead(pin, callback)

The parameters of this function are described as follows:

  • pin: BeagleBone pin identifier string
  • callback: Name of function that will be called automatically when analogRead() finishes.

The first parameter pin is used to find the corresponding sysfs file to read data from. Similar to the digitalRead() function, the analogRead() function is blocking. It waits till analog voltage is read and the ADC conversion is done. The Node execution engine freezes till this happens. So, there is need for an asynchronous callback function. The second parameter is the callback function, which will be called automatically when analogRead() completes. Similar to digitalRead(), this callback function gets called with a special object as parameter. This special object has property value, which is the read value on the ADC. It is a floating point number between 0 and 1 where 0 is mapped to 0V and 1 is mapped to 1.8V. The mathematical function of this mapping is f(x)=1.8x. So, if you get the value 0.5, then voltage at the input pin is 0.9V. This conversion needs to be done for all read values. Getting the voltage value is not sufficient. You need to convert that to the corresponding sensor value. The sensor datasheet needs to be referred to in order do this conversion. Based on this information we will write program to print the temperature using the TMP36 temperature sensor. Let's make our BeagleBone aware of the outside temperature.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.234.24