Integrating the Real Time Clock (RTC)
- The BeagleBone Black, like other single board computers, does not have an onboard clock with battery backup to preserve current date and time.
- Theory of the case being that if the application needs one the date and time can be gotten from:
- The network via NTP, or
- An optional battery backed Real Time Clock.
- Since ‘The App’ is not a network attached device the latter alternative has been implemented.
- Luckily the BBB does come with the necessary hooks to easily integrate an RTC (assuming it is one of a list that is supported).
- The RTC is connected using I2C and powered from the BBB as shown in the previous diagram.
- Once it is connected the time on the BBB is set using NTP while it is connected to the network – ‘ntpdate -b -s -u pool.ntp.org’
- Once the BBB is running the correct time it can be downloaded to the RTC – ‘hwclock -f /dev/rtc1 –w’
- From that point the BBB can get the time from the RTC rather than the network – ‘hwclock -f /dev/rtc1 –s’
- It is not essential that the BBB support a real time clock for operation of ‘The App’, however, doing so will allow files to be date and time stamped.
Integrating the Analog Digital Convertor (ADC)
- The BBB has an onboard ADC, however, the entire ADC is allocated to the LCD cape!
- Given this I have integrated an off board ADC, one that was designed for the Raspberry Pi, but that works with anything that communicates via I2C.
- Wiring is shown by the preceding diagram and consists of five lines, two power, two for I2C, and a single line from our input source.
- One of the disadvantages with the onboard ADC was it’s operating range for input (-1.8 to +1.8v). The Pi ADC operates from 0-5v which better suits our requirement.
- The bulk of the work of talking to an I2C device is done by a library installed via NPM – ‘npm install i2c’. This library is available as part of Bonescript which ships with the BBB or as an install from npm per the above (which is what I am using since I don't have a need for the rest of Bonescript.
- The code to access the ADC is shown below with some comments.
Initialize the interface library |
var i2c = require('i2c');
|
Setup the address and command for the ADC. The command translates to 12 bit resolution, gain of 1, in continuous mode |
var address = 0x6A; var command = 0x90;
|
Use the library loaded above to instantiate a new wire (I2C) interface |
var wire = new i2c(address,
{device: '/dev/i2c-1', debug: false});
|
Setup an interval to capture observations. 5ms is as fast as we can go |
intervalId = setInterval(function() {
observeCallback();
}, 5);
|
Here is the callback for the above interval. This function actually calls on the I2C interface to get four bytes of data from the ADC |
function observeCallback() {
wire.readBytes(command, 4,
function(err, res) {
observe(res);
});
}
|
Within the observe function the two bytes of data that actually contain our reading are bit shifted into a measurement |
function observe(buffer) {
var voltageIn =
(buffer[0] << 8) + buffer[1];
voltageIn = (voltageIn /
2048) * 5.0;
|
There is a huge caveat on the above code. Namely that when embedded within my application it will, at some point, cause a Segmentation Fault. In a later post I will describe my solution to this rather fatal problem!
No comments:
Post a Comment