Exploring the tiny $9 C.H.I.P. computer

GPIOs for Projects

The C.H.I.P. comes with no fewer than 80 pins (Figure 9), most of which are programmable, and you can use them for your hardware projects. The most obviously useful pins are the XIO pins (referred to as "GPIOs" in the C.H.I.P.'s documentation pages). Although the official documentation only lists 8 GPIOs (XIO-P0 through XIO-P7), in fact, you can use practically everything that is not a GND pin or supplies a constant voltage (VCC). For example, you can hook up an LED to an LCD pin and still see it blink.

Figure 9: Complete pin layout for the C.H.I.P.

The steps you need to take to read from or write to a GPIO are as follows:

1. Export the GPIO so a special directory gets created under /sys/class/gpio.

2. Set the GPIO's direction file to in for reading (the default) or out for writing.

3. Get the GPIO's value if you're reading, or push a   or a 1 into the GPIO's value file to set it to LOW or HIGH.

But, before you try it out, you have to figure out what addresses your Linux has assigned to the XIO pins, because the addresses vary from kernel version to kernel version.

Execute the following in a terminal:

cat /sys/class/gpio/gpiochip*/base
0
1016

If you are using the regular desktop firmware with kernel 4.4, the most likely output will be telling you that XIO-P0 (pin 13 on U14 as shown in Figure 9) is address 1016 in your system. That means XIO-P1 will be mapped to port 1017, XIO-P2 will be mapped to 1018, and so on. To export port XIO-P0, so you can start using it for input and output, do the following:

sudo echo 1016 > /sys/class/gpio/export

This command will create the directory /sys/class/gpio/gpio1016, which contains, among other things, the port's direction and value files.

If you read from direction with cat /sys/class/gpio/gpio1016/direction, it will probably output in, which means it is configured for input. To test it for output so you can blink an LED:

sudo sh -c 'echo out > /sys/class/gpio/gpio1016/direction'

Connect your LED's anode (+) leg to pin XIO-P0 and its cathode (-) leg to any GND pin. To light up the LED, push a 1 into the GPIO's value file:

sudo sh -c 'echo 1 > /sys/class/gpio/gpio1016/value'

If you can't see it, do it in dark room, since the LED lights up very faintly.

Experimenting, we discovered we got better results using the LCD pins instead of the regular XIO pins. The LCD pins – pins 17, 19 to 38, and 40 on U13 (see Figure 9) – are mapped to ports 98 through 123 in the operating system [3]. Connecting the anode leg of your LED to LCD-D2 (pin 17 on U13) would be similar to the preceding example, but you would change 1016 for 98 (Figure 10).

Figure 10: Using one of the LCD pins gave a much brighter light. In this photo, the white jumper cable is connected to LCD-D23, and the black jumper cable is connected to GND.

To read a digital input from, say, a button connected to XIO-P0, you would set the port to in:

sudo sh -c 'echo in > /sys/class/gpio/gpio1016/direction'

And then get the contents of value:

cat /sys/class/gpio/gpio1016/value

If all this seems cumbersome, you're right, it is. Fortunately, the very active C.H.I.P. community has a working Python module you can use instead.

The C.H.I.P. also comes with a PWM pin (pin 18 on U13 and mapped to port 34) , which is something even the latest iterations of the Rasp Pi lack. This special pin allows you to modulate your output. You can only send a 0 or a 1 over a digital pin, but you can send between 0 and 255 to a PWM. You can use this feature to control the brightness of your LED or the speed of a motor, for example.

Surprisingly, you also have an analog input, which is another feature you won't find on a Pi. Pin 11 on U14 (labeled LRADC in Figure 9) can read from thermometers, potentiometers, and so on. However, at the moment of writing, there is no fully functioning driver for operating the analog input, although, by the time this issue reaches the newsstand, a driver might be available [4].

C.H.I.P. Pocket

The LCD pins are put to great use with the Pocket C.H.I.P. The Pocket C.H.I.P., which is a bit like an antique Game Boy on steroids (Figure 11), has a full keyboard and a touch screen. Also, along the top, you have access to many of the pins from the C.H.I.P.'s GPIOs. Plug the C.H.I.P. into the back, and you have a portable C.H.I.P. workstation.

Figure 11: The Pocket version of the firmware comes with a touch-friendly interface.

You can run the standard firmware with the Pocket, but you will have to plug an external mouse into the C.H.I.P., since the touch screen won't work properly. Adding a mouse kind of defeats the point of having a touch screen at all. A better idea is to flash the C.H.I.P. with the Pocket's own, especially customized, Pocket firmware. This firmware configures the touch screen properly and comes with a graphic, touch-friendly interface, with big, easy-to-tap icons.

The Pocket can do most of the things you can do using the regular firmware, including writing documents using AbiWord, surfing the web using IceWeasel, or programming your GPIOs from a terminal, but where the Pocket really shines is with games. You'll find some really lovely retro, 8-bit games that are a delight to play on the Pocket. The aesthetics of the Pocket are delightfully reminiscent of the 80s.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content