Slideshow shadow

Starting Raspberry Pi: WiFi, ssh and GPIO

June 18, 2013 in Raspberry Pi

This is a short post on setting up the Raspberry Pi (RPi) for development for the first time.

Being that the RPi is a full-fledged mini computer, it seems prudent to get a case rather than sticking wires all over it. Here’s a case that I picked up from ebay:

RPi case

It’s also very helpful to get a power adapter. This one’s from Samsung (5V, 700 mA) and connects to the micro USB port:

RPi power adapter

Next, you need to install an Operating System for your RPi. What you do is to download the latest “Wheezy” from the RPi official site, and write that image on to an SD card. Here is a nice video that will help you with this part:

For the first-time setup, it is nice to have a keyboard and mouse. I got this wireless keyboard with built-in trackpad for this purpose, and it was plug & play installation with Wheezy.

RPi wireless keyboard

Internet is the next priority, and this USB WiFi adapter works great:

RPi WiFi adapter

You can use your TV as the RPi monitor, as long as it has an HDMI cable. Here’s the my RPi connected to the TV:

RPi TV

The first thing I need for RPi development is to have remote access to it. I don’t want to sit in front of my TV and use a miniature keyboard to do coding. So the next thing is to set up ssh on the Pi so I can remote login. For this, we need to get a static IP setup for the Pi. You can read more about this here:

http://elinux.org/RPi_Remote_Access

Here is what my network configuration file looks like:


pi@raspberrypi /etc/network $ cat interfaces
auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet static
address 192.168.4.31
netmask 255.255.255.0
gateway 192.168.4.1

Now, you can login from your machine (in my case, a Mac) as follows:

$ ssh pi@192.168.4.31

Note that the default password for the user “pi” is “raspberry”.

You can also display GUI from the RPi on to your machine as long as you have an X-server running. Start the session with:

$ ssh -X pi@192.168.4.31

Now, if you type:

$midori

The RPi browser window will popup on your remote machine, if you have the X-server setup correctly. But this is painfully slow, which is why I stick to ssh and the command line.

Python comes pre-installed with the RPi, which is great. But to use the GPIO pins with Python, you still need to get the appropriate library. Here is how you can do it:

$sudo apt-get update
$sudo apt-get install python-dev

Now download the Python GPIO library from:

http://code.google.com/p/raspberry-gpio-python

Download, unzip, untar, cd into the directory and do:

$sudo python setup.py install

Now you are all set to talk to external hardware with Python!

As a simple test of the GPIO, I am hooking up an LED to pin 18 (GPIO5) of the RPi.

RPi GPIO

Here is the code to make this LED blink:

Your RPi is all set. Just power it on, and since the WiFi adapter puts it on your network, you can remote login any time from your machine and work on your project.

Talking to Ultrasonic Distance Sensor HC-SR04 using an ATtiny84

June 12, 2013 in AVR Programming

In a previous post, I talked about interfacing an Arduino with the HC-SR04 ultrasonic distance sensor. This time, I will do the same, but using an ATtiny84 and C code – no Arduino hardware or libraries.

The HC-SR04 works as follows:

  • Send a 10us HIGH pulse on the Trigger pin.
  • The sensor sends out a ā€œsonic burstā€ of 8 cycles.
  • Listen to the Echo pin, and the duration of the next HIGH signal will give you the time taken by the sound to go back and forth from sensor to target.

Here, the PB0 pin is used to send out the 10 us pulse. To measure the width of the echo pulse, we can use a pin-change interrupt and a timer. Here is the idea:

  • Setup pin change interrupt PCINT0 so that any logical change on pin will cause an interrupt.
  • Send a 10 us pulse to the trigger pin.
  • Loop till the PCINT0 interrupt sets a flag to indicate that measurement is done.
  • In the PCINT0 interrupt, start an 8-bit timer when you see a rising edge – ie., the echo pulse has gone from low to high. The 8-bit timer is setup to use the overflow interrupt.
  • The 8-bit counter overflows every time it reaches 255, and so when that interrupt fires, we add 255 to a running 32-bit counter value.
  • In the PCINT0 interrupt, stop 8-bit timer when you see a falling edge – ie., the echo pulse has gone from high to low. Update 32 bit count, and set flag to indicate that the measurement is done.
  • The measured pulse width is in terms of a counter value, and we can convert that into seconds, since we know the clock speed. This time value is then used to calculate the distance.

The distance is then sent using serial communications on pin PB1 – I’ve covered this part in a previous post. This is also the reason we cannot use the 16-bit timer to measure the pulse width – it’s already being used for serial communications. Plus it’s fun to learn how to use the 8-bit timer to count large values, right? ;-)

Here is the schematic:

attiny84-hcsr04

And the breadboard looks like this:

attiny84-hcsr04

The full C code is listed below:

This is the Makefile that goes along with the above code. It is similar to the ones posted before – I’ve just added some extra linker flags to support full printf formatting.

https://gist.github.com/electronut/5763929

And here is the Python code used to plot the data:

https://gist.github.com/electronut/5730160

The Python code is a minor modification to what I posted before on the subject.

Serial Communications with the ATtiny84

June 4, 2013 in AVR Programming

In a previous post, I talked about serial communications with an ATmega168. But that chip has USART – hardware support for serial communications. But what about the tinyAVRs? As continuation of my last post on setting up the ATtiny84 for programming, this time, I will talk about sending data from an ATtiny84 to a computer using serial communications.

First, I recommend that you watch this fun and informative video on serial communications by Pete from Sparkfun, USA:

So, now that we have some idea about serial communications, let’s talk about implementing a transmit only (TX) for the ATtiny84. Here is our scheme:

Serial Comms

The above image shows the transmission of a byte (with value 0×95). The value is sent one bit at a time, starting with the least significant bit (LSB), every 1/9600 seconds, thus giving us a baud rate of 9600. The data format for a packet for the 9600 baud 8-N-1 serial connection is as follows:

start(low)-[0]-[1]-[2]-[3]-[4]-[5]-[6]-[7]-stop(high)-idle(high)-idle(high)

(In the above, the 2 idle bits at the end are not strictly necessary.)

So how do we implement sending a bit every 1/9600 seconds? For this, we use the 16-bit timer (Timer1) of the Attiny84 in CTC mode. The value for the top of the counter is calculated as shown in the image above.

In the ISR for the timer, we keep track of the current data byte and the current bit being sent, and keep setting the output pin high/low as appropriate.

Here is the code that implements the above ideas.

The Makefile is the same as we used in the ATtiny84 introduction post:

https://gist.github.com/electronut/5689137

This is the schematic for wiring it up:

IMG_1499-2

Here is what the breadboard setup looks like:

ATtiny TX

And here is the output from CoolTerm. It was surprisingly easy to get this working. ;-)

attiny84-coolterm

Getting Started with the Atmel ATtiny84 Microcontroller

June 1, 2013 in AVR Programming

Like many folks, I got introduced to microcontrollers through the Arduino platform. The ease of setup, simple language syntax and the availability of a huge number libraries makes Arduino a very attractive choice to prototype your hardware projects. But after you get a grip on your project, sometimes it’s worth asking if you really need the power and the prototyping ergonomics of an Arduino, especially if all you are doing is reading a few sensors and turning on a few LEDs.

Atmel has a line of microcontrollers called tinyAVR which are little microcontrollers which can do a lot of the work that an Arduino does in a much more compact form. Take a look at the wikipedia page that compares the capabilities of the tinyAVRs.

In this post, I’ll be setting up to program an Atmel ATtiny84, a chip that I will be using for most of my ATtiny projects. I picked this 14-pin chip rather than the very popular 8-pin ATtiny85 because the former has a few more I/O pins which could come in handy for many projects. Here is a pin comparison between these 2 chips.

[From Atmel datasheets, for illustrative purpose.]
84-85-comparison

I will be programming the ATtiny85 in C, using the free AVR-GCC and AVRDUDE tools. Windows users please read the Adafruit guide to set up the AVR tools. Mac users (like me, for instance) can use the free CrossPack suite for AVR programming.

Next, you need a programmer. I recommend the Sparkfun Pocket AVR programmer, which I use.

The setup is really simple – just connect the VCC, MISO, MOSI, SCK, RESET, and GND pins from the chip to the same pins on the programmer. The ugly looking small PCB you see below is my homemade version of the Sparkfun AVR programming adapter – not a requirement, it just makes it convenient to connect the 6-pin plug easily to a breadboard.

Note that you need not connect the VCC pin to the programmer, if you are powering the circuit yourself. There is a small switch on the programmer in case you decide to go that route.

Here is my programming setup:

Programming ATtiny84

Once you program the chip, you don’t need the programmer, and here you can see the chip working directly from the 3V battery:

ATtiny84

Here is the source code – very simple:

Here is the Makefile:

To upload the program to your chip, connect the programmer to the USB port of your computer, and run ‘make install’ in a shell in the relevant project directory. If all goes well, you will see a happy blinking LED.

So now you know how to program an ATtiny84. But to really understand the chip, you have to read the Atmel datasheet (just search for ‘ATtiny84 datasheet’ on the net) – there is no way around it. It’s complex and can seem incomprehensible in the beginning, but do persist, and in the end, I assure you that it will be much more rewarding than blindly using libraries that someone else wrote.

Good luck to you in starting out with the tinyAVRs, and watch electronut.in for upcoming projects that use these chips.

Soldering Accelerometer MMA7660 in DFN-10 package using a Hot Air Rework Station

May 30, 2013 in SMD

MMA7660-coin-comparison

Freescale makes some nifty accelerometer chips, like the popular MMA8452, for which Sparkfun, USA sells a breakout board. But unfortunately (for hobbyists) they all come in hard-to-solder tiny little packages, like the 16 pin QFN which is just 3 mm x 3mm across.

After a few unsuccessful attempts to solder the MMA8452 chip at home, I found a better candidate – the MMA7660 which has a DFN-10 package. As you can see from the schematic below, the latter has only 10 pins, and the pins extend to the sides, which makes it easier to solder.

MMA7660 vs MMA8452
[From Freescale MMA8452/MMA7660 datasheets, for illustrative purpose.]

I was able to successfully solder the MMA7660 on to DFN-10 prototype board using a hot air rework station.

I basically followed the technique shown in the video below:

  1. Apply flux and tin the pads.
  2. Place chip on tinned pads – align correctly, apply flux.
  3. Heat the chip with the hot air gun.
  4. Solder exposed pins on the sides.
  5. Reheat the chip with hot air gun to reflow the solder.
  6. At each stage, inspect pads with magnifier for shorts, and use flux generously.

Sjaak solders a QFN chip, attempts a BGA chip:

A couple of photos of the chip after soldering:

MMA 7660 MMA 7660 Magnified

So why torture yourself soldering these microscopic chips when you can buy breakout boards?

A few reasons to consider, especially if you are trying to make a product that you might sell:

  • Cost: The breakout board is usually 4-5 times more expensive than buying the chip.
  • Size: If you are designing a PCB yourself, it’s going to be much more compact if you just use the chip and not the whole breakout board.
  • Bragging Rights: When you walk into a bar and announce that you hand soldered a DFN-10 chip, that will surely make you popular. Right? Right? ;-)

Using Ultrasonic Distance Sensor Module HC-SR04 with an Arduino

May 28, 2013 in Arduino

I just got hold of an HC-SR04 Ultrasonic Sensor Module. This is a short post on hooking it up to an Arduino Uno and getting distance information from it.

The sensor has 4 pins – VCC (5V), GND, Trigger and Echo. Looking at the datasheet for HC-SR04, the way it work is:

  • Send a 10us HIGH pulse on the Trigger pin.
  • The sensor sends out a “sonic burst” of 8 cycles.
  • Listen to the Echo pin, and the duration of the next HIGH signal will give you the time taken by the sound to go back and forth from sensor to target.

This is what it looks like hooked to the Arduino:

HC-SR04-Arduino

And here is the Arduino code:

This is what the distance plot looks like as I walk to towards the sensor. This is done using Python and matplotlib – please take a look at my post on plotting real-time data with Python.

distance-plot

The datasheet says that the sensor has a range of 2cm to 400 cm, and an accuracy of 3mm. The target needs to be 0.5 sq m, so it’s not going to be very useful to scan for small objects. But it could be useful in many other situations. The sensor is cheap – about Rs. 250, and I could get it from ebay.in easily.

ATmega168 Power Save Mode and Pin Change Interrupt

May 26, 2013 in AVR Programming

Most times your microcontroller is running in a loop, waiting for something to happen – like a button press. All this while it is consuming power, and this could be an issue, especially if you are running the circuit from a battery. To counter this problem, there are ways of reducing the power consumption of your chip. Here are the various “sleep modes” supported by the ATmega168.

[Table reproduced from Atmel ATmega168 datasheet for illustrative purpose]

ATmega168-sleep-modes

In a previous post about an Ambient Light sensor using an Op-Amp Comparator, I mentioned that we would be using the op-amp output to “wake up” an ATmega168. So here, we will be using the “power-save” mode from the above table, and use a pin-change interrupt to wake up from this sleep mode.

The setup used is shown below in the 2 schematics I had posted before. The output Vo from the op-amp should be connected to the pin 14 of the ATmega168 (PCINT0/PB0). Also, in this case, we are not using serial output, so you can remove the connections to the FTDI adapter.

IMG_1415

IMG_1412

Here is what the circuit looks like, hooked up on a breadboard:

ATmega168-power-save-breadboard

This is the how the program works:

  • When the program starts, start the 16-bit timer, set to fire an interrupt every 3 seconds. Start the timer only if pin 14 is low. (This means that the light level is high in our sensor circuit.).
  • When the chip is not sleeping, it will be flashing an LED attached to pin 6.
  • When the 16-bit timer interrupt happens, set a flag (a volatile variable) so that the main loop reads this flag and puts the chip to sleep mode. It will also turn the LED off at this point.
  • Set up a pin change interrupt on PCINT0 for any logical change in input. When the interrupt fires, if it was caused by a rising edge (ie., light to dark), wake up the chip, and stop the 16-bit timer. If the interrupt was caused by a falling edge (ie., dark to light), re-start the 16-bit timer.

You can read about pin-change interrupts and the 16-bit timer in the Atmel ATmega168 datasheet.

The complete code listing is below. The opening comments in the code also have all the required build commands.

Plotting real-time data from Arduino using Python

May 24, 2013 in Arduino

Arduino is fantastic as an intermediary between your computer and a raw electronic circuit. Using the serial interface, you can retrieve information from sensors attached to your Arduino. (You can also send information via the serial interface to actuate circuits and devices (LEDs, relays, servos, etc.) connected to your Arduino.) Once you have the data in your computer, you can do all sorts of things with it – analyze it, display it, or share it on the internet, for instance.

In this post, I will be reading and displaying analog data from a pair of LDRs connected to an Arduino. Here is the schematic:

IMG_1443

Here is how you hook it up to the Arduino:

IMG_1439

The Arduino sketch is very simple – it just reads the values from analog pins A0 and A1 (in the range [0, 1023]) and prints it to the serial port. Here is the code:

The serial port sends values in the format:

512 300
513 280
400 200
...

On the computer side, I need to read these values, and plot them as a function of time. I am using Python and the Matplotlib library for this. I wanted to display this as a scrolling graph that moves to the right as data keeps coming in. For that, I am using the Python deque class to keep and update a fixed number of data points for each time frame.

You can see the full implementation here:

And here is what the plot looks like. It scrolls to the right as data keeps coming in.

plot

Ambient Light sensor using an Op-Amp Comparator

May 22, 2013 in Circuits

Say you have a microcontroller circuit that does something when it goes dark. To save power, you want to put the chip to sleep when the ambient light drops below a certain level. One way to do this is using an LDR and an op-amp comparator.

Here is the schematic:

IMG_1415

In the above circuit, the reference voltage at the non-inverting terminal of the op-amp is VCC/2. When it’s dark, the LDR has a high resistance (over 20K), and the voltage at the inverting terminal (pin 3) is going to be less than VCC/2. Hence, the output of the op-amp will go to high when it is dark. When sufficient light falls on the LDR, its resistance falls, and the voltage at the inverting terminal (pin 2) exceeds VCC/2. At this point, the op-amp output goes low. We can control the threshold at which it goes from low to high by adjusting the potentiometer R1.

Here is what the circuit looks like on a breadboard. The supply is 5V regulated.

IMG_1416

In this case, I am using the LM358 – a very popular general-purpose Op-Amp IC. This works fine for our purpose, but do note that there are dedicated comparator ICs with better switching characteristics for critical applications.

In my next post, I will describe how to hook this up to an ATmega168 and wake it up from power-save mode.

Serial Communications with the ATmega168

May 20, 2013 in Circuits

The first thing you do when you learn a new programming language or platform is to write a “hello world” application. This requires something like a “printf” function. That’s not so straightforward when it comes to microcontrollers – where will the output of the “printf” go? That’s where serial communications come in. Arduino users have it easy – they just need to use Serial.print(). But the situation is not so bad if you are using a standalone microcontroller – just choose a chip like ATmega168 which has USART - hardware support for serial communications.

The code needed to transmit serial data is very simple, and the datasheet has most of what you need:

Here is the schematic of a simple setup that will let you send debug data (strings) from an ATmega168 to your computer. If you are completely new to AVR programming, I recommend that you read Hackaday’s tutorial on the subject. Note how the TX/RX lines are flipped when you connect it from the ATmega168 to the FTDI adapter.

A few things to remember in order to for this to work correctly (the Makefile in the GitHub link below takes care of all this):

  • For getting a 9600 baud rate, the chip needs to run at 8 MHz, and for this, you need to unset theĀ  CKDIV8 fuse.
  • For full sprintf formatting support, some additional flags are needed in the linker.

 

IMG_1412

 

Here is a photo of the setup that I used:

 

IMG_1411

 

Here is what the output looks like on CoolTerm, a serial monitor that I use on my Mac:

 

coolterm

 

Having a “printf” function is very handy for debugging your projects – so choose a chip that will let you support this functionality without too much pain.

Downloads

Here is all the code used in this project – do pay attention to the flags in the Makefile.

https://github.com/electronut/atmega168-serial-hello