Digital I/O functions - pinMode() and digitalWrite()

Digital components can be of an input or output type. A push button is an input type digital component. It does not make sense to write on a push button. LEDs and buzzers are output type digital components. We can program a digital I/O pin on BeagleBone to communicate with both the input and output type of components. However, it has to be specified first whether we are going to use that pin as an input or output pin. Once specified, the pin acts in that direction only. We will learn in detail about this in the next chapter. To blink an LED, we need to specify that we are going to use that pin as the output. Then only we can program it to turn on or off.

Debian on BeagleBone comes with a preinstalled Node.js library called BoneScript. It provides simple Arduino-like functions to interface the BeagleBone hardware. So, it becomes easier to program in BoneScript if there is prior experience in programming Arduino. The Bone101 page that we studied in the previous chapter has detailed information and many examples on BoneScript APIs. This is an open source library. You can get the source code at https://github.com/jadonk/bonescript.

Before doing the actual programming, let's learn essential functions required to communicate with the digital components. BoneScript provides you the pinMode() function to set the direction of the specified pin. The direction can be the input or output. Once the direction is set, the pin will work in that direction only. If you want to change the direction, you need to use the pinMode() function again. Here is a prototype of the pinMode() function:

pinMode(pin, direction)

Where

pin – BeagleBone pin identifier string

direction – Input or Output direction

The pinMode() function takes the name of the pin to be selected as the first argument. The second argument indicates if it is connected to the Input or Output type component. Once the Output type is specified, we can make the pin high or low. This is achieved by the BoneScript digitalWrite() function. Here is a prototype of the digitalWrite() function:

digitalWrite(pin, value)

Where

pin – BeagleBone pin identifier string

value – HIGH or LOW

This function takes the name of the selected pin as the first argument and the second argument as a HIGH or LOW value. It writes this value on the specified pin. This will be clear when we write a program using these functions. These functions are similar to the pinMode() and digitalWrite() functions from the Arduino world.

Note

Actually, the pinMode() function takes more than two arguments. However, they are optional and not required for our current goal of blinking LEDs. We will continue studying only required parts to avoid confusion throughout the book.

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

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