Chapter 4. Controlling LED Using a Push Button

In the previous chapter, we wrote programs that were instructing LEDs to be on or off in pattern. LEDs are output devices. BeagleBone provides some information to us through LEDs, for example, by default the USER1 LED is turned on when the SD card is accessed. Sometimes, we want to give some information to BeagleBone, such as send an e-mail or send SMS. We need to use an input electronic component that will pass our information to BeagleBone. The most common input component used in the embedded world is the push button. BeagleBone has three small push buttons onboard. But they are already assigned to system critical work. So, we will use an external push button to convey our message to BeagleBone in this chapter.

Here are the topics that will be covered in this chapter:

  • Reading from digital components
  • Push button circuit setup
  • Program to read from push button
  • Reading via interrupts
  • Push button LED circuit setup
  • Program to control LED by push button

Reading from digital components

Push buttons are digital. They are either pressed or released. There are some touch buttons that work just like push buttons. They generate a HIGH signal when touched. There are some sensors that work as HIGH/LOW digital input devices. They just give information if the threshold value is reached or not. For example, some IR sensors come with a digital pin that gives only a HIGH/LOW value if it detects an obstacle in between a specific distance. Any electronic device capable of generating a HIGH/LOW signal can be treated as an input device. This signal is sensed at the connected BeagleBone pin and the input value is interpreted as HIGH/LOW. These are all digital input devices.

The BoneScript library provides the function digitalRead() to read from digital components. The function digitalRead() requires the pin to be initialized first using the function pinMode() similar to digitalWrite(). But it should be initialized for input unlike digitalWrite(). Here is the prototype of the digitalRead() function:

digitalRead( pin, callback)

The parameters are as follows:

  • pin: The BeagleBone pin identifier string
  • callback: Name of function which will be called automatically when digitalRead() finishes.

The function digitalRead() internally reads from the sysfs file that corresponds to the pin. This may take some time to return and the Node will be blocked until then. This is a blocking function and needs to be done in an asynchronous way. So, the second parameter is a callback function that will be called automatically when digitalRead() completes. This callback function gets called with a special object as parameter. This special object has the property value, which tells us if the pin is HIGH or LOW. This is much like a structure and its member if you are coming from a C background. You will get a clear idea once you go through the program. Before writing the program, we need to set up a circuit for the program.

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

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