Homework 4: Linux GPIO Interface

  1. Use your Raspberry-Pi to work on this project.

You will need a breadboard, an LED, some resistors, and some wire for this homework. In class I handed out some female/male hookup wires for connecting the Pi to the breadboard. You should source the remaining parts yourself, although if for some reason you cannot let me know and we can arrange something.
• Download the code from: http://web.eece.maine.edu/~vweaver/classes/ece471/ece471_hw4_code.tar.gz and copy it to the Raspberry-Pi.
• Uncompress/unpack it: tar -xzvf ece471_hw4_code.tar.gz
• Change into the ece471_hw4_code directory: cd ece471_hw4_code

2. Hook up an LED to a GPIO pin (3 points total)

  • Hook up an LED to GPIO18. Figure 2 shows the appropriate circuit and Figure 1 and Table 1 might be helpful.
  • Modify blink_led.c so that it makes the LED blink. It should blink with a pattern of 0.5 seconds on, 0.5 seconds off, repeating forever (on Linux to exit out of a program in an infinite loop you can press control-C). Be sure you have the timing right!
Table 1: Raspberry Pi Header Pinout
Figure 2: LED connected to GPIO18.
  • As a reminder:
    • Write your code in C.
    • Make sure the code compiles *without warnings*.
    • Write the GPIO number (in ASCII text) to /sys/class/gpio/export to enable it iv.Write “ out” to /sys/class/gpio/gpio18/direction to make it an output. v.Write “ 1” or “0” (ASCII characters, not integers) to /sys/class/gpio/gpio18/value

to change the value.

vi. usleep() can be used to sleep a certain number of microseconds. vii.Comment your code!

  • Be sure to check for errors (especially at open time)! If you detect an error, print a message and then clean up and exit the program.
  • You will probably have to run your code either with root permissions, or else using the  sudo

utility which temporarily runs a program with root permissions, i.e. sudo ./blink_led

Figure 3: Switch connected to GPIO.

3. Read input from a switch (3 points total)

  1. Connect GPIO17 to a switch as shown in Figure 3. If you do not have a switch, you can instead just use a piece of wire connected to 3.3V and touch the other end to the proper pin.
  2. Modify read_switch.c so that it loops forever waiting for keypress events: it should print to the screen when the key is pressed and when it is released. Each time you press and release the switch it should only print *two* messages, one when the key is pressed and one when it is released. You can implement this by polling (constantly reading the input in a loop) you do not need to use the fancier interrupts/poll() interface. You will, however, need to de-bounce the switch in software.

4. Something cool: (1 point total)

Edit gpio_extra.c and do something cool. Put a short description of what you did in the README. Here are some suggestions:

  • Hook up a second LED and have them blink alternately.
    • Have the switch toggle the LED on and off.
    • Have the LED blink a Morse code message.

5. Questions to Answer: (2 points total)

Put the answer to these in the README.

  • Why is it good to use usleep() rather than a busy loop?
  • How does having an operating system make life easier when programming GPIOs?
  • What limitations does the operating system impose when programming GPIOs that would not be there if you were writing directly to the hardware?
  • Would you consider a web browser to be part of an operating system?

6. Linux Command Line Exploration (1 point total)

Try out the dmesg program. This shows all of the system boot messages. Try piping the output into

less so you can easily scroll back and look at the messages:

dmesg | less

  • You can use grep to search for text. Find out what your machine type is by running:

dmesg | grep Machine

Report your machine type in the README file.

  • You can use uname to find out more about your system. Run:

uname -a

and report your kernel version in the README file.

  • You can use df to find out how much disk is free. Run:

df -h

and report the free space on your rootfs filesystem. (d)What does the -h option to the df command do?

  • Submitting your work.

Run make submit which will create hw4_submit.tar.gz

You can verify the contents with tar -tzvf hw4_submit.tar.gz


e-mail the hw4_submit.tar.gz file to me by the homework deadline. Be sure to send the proper file!

Source: Homework 4: Linux GPIO Interface

Scroll to Top
Read previous post:

Würth Elektronik announced the Bluetooth Low Energy 5.1 module Proteus-III and the proprietary version Thyone-I. The modules, measuring only 8 × 12 × 2 mm,...