Showing posts with label AVR. Show all posts
Showing posts with label AVR. Show all posts

Wednesday, 9 March 2016

Arduino 16x2 LCD/Keypad Shield

Many embedded projects benefit from a user interface, and shields with a 16x2 LCD and a 5 button keypad are popular with Arduino users. Most are almost exactly like this one. As with many Arduino peripherals, they are very inexpensive - less than 6 GBP at the time of writing for the one I chose with first class postage within the UK, less still if you can wait for one from China.

Arduino LCD/keypad Shield
Arduino LCD/keypad Shield
Boards such as this are often referred to as "1602A" type, with various manufacturers adding various prefixes and suffixes. They all seem to be based on the Hitachi HD44780 LCD controller (or a compatible clone). And most seem to use a 4-bit parallel interface to this controller (which can also support 8-bit operation). The keypad buttons are wired with a resistor ladder between the power rails so that a unique voltage corresponding to each button is delivered into one of the MCU's ADC inputs.

There are various examples and drivers available for 1602A display/keypad boards running on the Arduino, but I wanted to understand it from first principles, so I coded my own simple driver in C after studying the controller datasheet and the board schematic. This allows you to clear and write strings to the display, and read debounced key presses. I wrote a quick demo program in C using Atmel Studio 7 for this which you can download here.

Note that the documentation is not reliable when it comes to the character set burned into the controller's ROM. The upper 128 character codes in my module did not match the data sheet, so - for example - I had to create a user defined character to get a "degree" symbol for the temperature reading shown in the above picture.

Here's a video showing the demo program running:


LED Brightness Control from an Arduino with PWM

I wanted to be able to control the brightness of an LED backlight in some pushbuttons. The AVR microcontroller on an Arduino has hardware timers with a Pulse Width Modulation (PWM) feature that allows you to send a regular stream of pulses out of a pin with control of the ratio of on time to off time (the mark/space ratio). This is a well established way of controlling LED brightness.

After setting up the AVR timer correctly, you write a value to its output comparison register (OCR) to control the mark/space ratio. A minor catch is that the ratio can be varied between 1:255 and 256:0 (for the 8-bit case) so the output can be fully on, but not quite fully off - indeed you see a perceptible but faint glow from the LED when the OCR is written with zero. My fix to this was to adjust things so the mark/space ratio can be varied between 0:256 and 255:1; the fact that "fully on" is no longer quite possible is imperceptible. This is accomplished by writing the brightness level minus one to the OCR, and treating zero as a special case - by turning off the output pin (via its Data Direction Register), relying on an external pulldown to keep the pin low.

I built a very simple driver circuit on a breadboard with a 2N7000 MOSFET; the Arduino drives its gate, which is pulled down with a 10k resistor. The source is grounded and the drain connects to the LED cathode. The LED anode connects to +12VDC via a 470R resistor which sets the forward current at about 20mA (the forward voltage is about 3V).

I wrote a quick test in C using Atmel Studio 7 - you can download it from here. And here's a video of the result, showing a fade up, a fade down, a sharp on transition, and a sharp off transition:


The fading effect looks much better to me.

Tuesday, 8 March 2016

Arduino Real Time Clock

I needed an accurate real time clock for an Arduino based home automation project; many are I2C controlled and based on Maxim's DS3231 which is achieves an accuracy of about 1 minute per year (2ppm) through temperature compensation - a potentially useful side effect of this is that the part provides a temperature reading. Most boards also include an I2C EEPROM. I found a seller on eBay offering 3 units for under 5 GBP, including next day postage:

Real Time Clock and EEPROM board
Real Time Clock and EEPROM board

The board provides a header with 6 pins:
  • GND - ground
  • VCC - power (3.3V or 5V)
  • SCL - I2C clock
  • SDA - I2C data
  • SQW - programmable interrupt/square wave output
  • 32K - free running 32kHz clock output
There's a holder for a CR2032 coin cell on the reverse, to provide battery backup. Note that these are typically not included with the boards (there are international restrictions on shipping lithium batteries).

I hooked it up to my Arduino Mega 2560 with male-female flying leads: I wired GND and VCC to GND and 5V on the power header, SCL and SDA to their counterparts on the communication header, and the SQW output to the INT3 input.

I then wrote a quick program in to test the board (I prefer to code in C using Atmel's Studio for AVR microcontroller development, for more on this see here.) This used the nice, stable and simple I2C master driver written by Peter Fleury. Note that I ignored the board's EEPROM.

The program prints the current time, day, date and temperature once a second:

       
RTC test
time: 12:14.44   day: TUE   date: 08/03/2016   temperature: +24.00
time: 12:14.45   day: TUE   date: 08/03/2016   temperature: +24.00
time: 12:14.46   day: TUE   date: 08/03/2016   temperature: +24.00


The program uses the Arduino's USB serial port for console I/O, based on Mika Tuupola's recipe here. I use TeraTerm on a desktop Windows PC for this, connected to the Arduino's COM port at 115200 baud.

You can download a ZIP of the Atmel Studio 7 project from here.

Monday, 22 February 2016

AVR Studio 7 and the Arduino Mega 2560

I've heard of the Arduino family of microcontroller boards but have not worked with one until today. I decided to take a closer look because I do know the Atmel AVR, in particular the 8-bit mega family, and an Arduino is a reasonably cheap way to get one to experiment with. I chose an Arduino Mega 2560 board.

My AVR experience is based on various editions of Atmel's Studio IDE - so I wanted to find out how to use it with the Arduino hardware, ignoring the Arduino software as much as possible.

Here are the steps I followed to run a simple C program. My starting point was a fresh install of Atmel Studio (version 7.0.783) and the Arduino software (version 1.6.7) on a desktop PC running Windows 10 - and (of course) an Arduino Mega 2560 connected to the PC via USB, with it's green power LED on.

(Note that you can click on an image to see a full size / full resolution version.)

  • on the Start Page, select New Project...
    AVR Studio Start Page
    Atmel Studio 7 - Start Page
  • in the New Project dialog, select GCC C Executable Project, specify a Name and a Location then click OK
    Atmel Studio 7 - New Project dialog
    Atmel Studio 7 - New Project dialog
  • in the Device Selection dialog, select ATmega2560 (the type of AVR microcontroller used on the Arduino board) then click OK
    Atmel Studio 7 - Device Selection dialog
    Atmel Studio 7 - Device Selection dialog
  • a minimal, template project is created, ready for you to enter code...
    Atmel Studio 7 - Virgin Project
    Atmel Studio 7 - Virgin Project
  • choose test Properties (assuming your project is called test) from the Project menu, then select Toolchain, and under AVR/GNU C Compiler select Symbols
    Atmel Studio 7 - Project Properties tab
    Atmel Studio 7 - Project Properties tab
  • in the Defined Symbols (-D) box, click the Add Item button and enter F_CPU=16000000UL (this specifies the processor clock frequency in Hertz; for the Arduino Mega 2560 this is 16MHz - the "UL" suffix means Unsigned Long)
    Atmel Studio 7 - adding a defined symbol
    Atmel Studio 7 - adding a defined symbol
  • select the main.c tab and enter enough C code to see life signs - mine pulses the "L" LED once then twice, over and over
    Atmel Studio 7 - entering a simple C test program
    Atmel Studio 7 - entering a simple C test program
       
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
 DDRB |= (1 << PORTB7); // PORTB7 direction = out
 
 while (1)
 {
  
  // 1 pulse
  PORTB |= (1 << PORTB7); // PORTB7 hi = LED L on
  _delay_ms(500); // 0.5 sec
  PORTB &= ~(1 << PORTB7); // PORTB7 hi = LED L off
  
  // delay
  _delay_ms(2000); // 2 sec

  // 2 pulses
  PORTB |= (1 << PORTB7);
  _delay_ms(500);
  PORTB &= ~(1 << PORTB7);
  _delay_ms(500);
  PORTB |= (1 << PORTB7);
  _delay_ms(500);
  PORTB &= ~(1 << PORTB7);

  // delay
  _delay_ms(2000);
 }
}

  • now the interesting part - adding support for the Arduino's programming mechanism, which requires the use of the avrdude utility that is included in the Arduino software installation

    • first, identify the COM port that the Arduino is connected to by running the Device Manger - in my case it was COM3...
      Device Manager (Windows 10) showing Arduino COM port
      Device Manager (Windows 10) showing Arduino COM port
    • choose External Tools... from the Tools menu
      Atmel Studio 7 - External Tools dialog (before)
      Atmel Studio 7 - External Tools dialog (before)
    • in the External Tools dialog...
      • change the Title to Program
      • enter the following in the Command box:
               
        c:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude.exe
        
        
      • enter the following in the Arguments box, changing -P COM3 to match the COM port that the Arduino is connected to on your machine:
               
        -v -C"C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf" -p atmega2560 -c wiring -P COM3 -b 115200 -D -U flash:w:$(TargetDir)$(TargetName).hex:i
        
        
      • check the Use Output window box
      • click OK
        Atmel Studio 7 - External Tools dialog (after)
        Atmel Studio 7 - External Tools dialog (after)
    • there is now a Program option on the Tools menu which will flash the compiled code into the Arduino board - let's add a corresponding shortcut to the UI:
      • select Add or Remove Buttons to the right of No Tool, then Customize...
        Atmel Studio 7 - adding a custom button
        Atmel Studio 7 - adding a custom button
      • the Customize dialog appears:
        Atmel Studio 7 - Customize dialog
        Atmel Studio 7 - Customize dialog
      • click Add Command...
      • under Categories select Tools
      • under Commands scroll down and select External Command 1
        Atmel Studio 7 - Add Command dialog
        Atmel Studio 7 - Add Command dialog
    • click OK then Close the Customize dialog
    • there is now a Program button left of the device (ATmega2560) button
  • to build the code, choose Build Solution from the Build menu (or hit F7):
    Atmel Studio 7 - after build
    Atmel Studio 7 - after build
  • now program the board by clicking the Program button - this will produce some rapid progress messages from the avrdude programming utility in the Output pane:
    Atmel Studio 7 - after programming
    Atmel Studio 7 - after programming
  • voila - you should see the "L" LED blinking once then twice, repeatedly