Wednesday, 30 September 2015

Tiny Accelerometers Acquisition System

By Giovanni Carrera -

The aim of this project is to realize a small acquisition system of a triaxial accelerometer connected via serial interface to a pc or tablet, using an appropriate adapter as TTL/USB or TTL/Bluetooth.
However, this system can also be used for other sensors with the same output range.

The Circuit
I used the PIC microcontroller 16F688, with minor modifications to the firmware you can use another microcontroller with one UART, three channels ADC and two digital bit.
For the CPU clock I used a 18.432 MHz quartz, the reason is that this frequency is particularly suitable for serial baud rate generation. You can find it easily in a RS232 board for PC.
The following figure shows the schematic of this project.
A quadruple dip switch allows you to set the sample rate and the range of accelerometers. To optimize the input range of the ADC, I used the TL431 regulator as adjustable reference source with a good thermal stability. As indicated in the scheme, the trimmer must be adjusted for a 2.80 output voltage, the same output range of the sensors.  The serial out is TTL 5V level. The following picture shows the arrangement of the components on the board of my prototype.

 The card with the accelerometer module has been removed to better see the mounted components, while the photo below shows the accelerometer module mounted. I used the module DC-SS009 made by Sure Electronics, but you can use an equivalent board. It utilizes the chip MMA7260QT from Freescale Semiconductor.
The J3, 4 pin, connector is used to acquire external analog signals, in this case the accelerometer module must be removed. RS1 is a quadruple sil resistor array. For the voltage divider I used 1% tolerance resistors and a 20 turns trimpot.
For a pc connection I used a TTL to USB adapter, which also powers the system. Are particularly suitable even those contained in the same USB connector, you have to install its driver on the PC.

The software
I set prescaler of Timer#0 to 256 and TMR0= 76, so it overflows every 180 counts. With these settings and with the quartz used, you get an interrupt every 10 ms (f = 100 Hz). This is the base period to generate the sampling periods with appropriate multipliers.
Two dip switches (dip1, dip2) are used to select the sampling period multiplier in order to obtain the frequencies of 5, 10, 50 and 100 Hz. The other two dip switches set the full scale of accelerometers(1.5, 2, 4, 6 g) by acting directly on the chip MMA7260QT and are not controlled by the program.
The serial output is ASCII coded with 38400 baud rate. Every row contains the numbers (0-1023) corresponding to the accelerometers X, Y, Z.  At first, after the reset, the device transmits the selected period in milliseconds .
To get the data in physical quantity (g) it is necessary to make a calibration using the acceleration of gravity, therefore, the conversion may be done on the pc.  I compiled my program  in mikroPascal PRO for PIC (mikroElektronika,
The source and compiled files (hex format) can be downloaded at:

1)      “3 Axis Acceleration Sensor Board User’s Guide”, DC-SS009, Sure Electronics, 2007.

2)      “±1.5g - 6g Three Axis Low-g Micromachined Accelerometer”, MMA7260QT, Freescale Semiconductor, 2007.

Saturday, 19 September 2015

How to use the TFT display 2.2" QVGA with Arduino, Part 2/2.

Arduino applications with ILI9341 library (Part 2)

By Giovanni Carrera
SD wirings.
On the other side there is a 4-pin connector (not welded) for driving the SD card on the module, the following table shows the connections:
TFT board
J4 Pin
SD Sck

If you want to use the SD card, the pin SD Cs must be connected to D10,another free pin of Arduino, while the other SPI signals must be connected together with those of the TFT display because in the module they are separate.
In the examples of the library there is a program that loads bitmap images from the SD card, it has been tested successfully, after transferring the files to the SD and correct one of the names in the program. In the sketch was used pin D4 of Arduino for the chip select of SD, but this is already used for resetting the TFT, so you need to change the program and assign the Cs of SD pin D10 Arduino (pin 16 of CPU).

Example#1:  alphanumeric writing  on the TFT

I did not find any documents describing the graphic functions library Seeed ILI9341. Were extremely useful the examples of graphics and text applications provided with the library. From these sketches and the library functions, well enough commented, I have written the following sketch of the test:
/* Text writing program using ILI9341 library
   using size 2 fonts, 20 characters/line are available
   in this case 20 pixel are needed for spacing
   G. Carrera 3 gen 2013

#include <stdint.h>
#include <TFTv2.h>
#include <SPI.h>

void setup()
  TFT_BL_ON;      // turn on the TFT backlight
  Tft.TFTinit();  // initialize the TFT library
  //write a string, x=0, y=0, size:2, color: yellow
  Tft.drawString("Prova display TFT",0,0,2,YELLOW);
  Tft.drawString("Misure analogiche:",0,40,2,WHITE);

void loop()
  // to cancel the previous values, it covers with a black rectangle:
  Tft.fillRectangle(100, 80, 100, 120,BLACK);
  int dato = analogRead(A0);// read the channel #0
  //write an integer, position: x,y, size:3, color: red
  Tft.drawString("Ch0:",10,80,3, RED);
  Tft.drawNumber(dato, 100, 80, 3, GREEN);
  dato = analogRead(A1);// read the channel #1
  Tft.drawString("Ch1:",10,120,3, RED);
  Tft.drawNumber(dato, 100, 120, 3, GREEN);
  dato = analogRead(A2);// read the channel #2
  Tft.drawString("Ch2:",10,160,3, RED);
  Tft.drawNumber(dato, 100, 160, 3, GREEN);
I have tried to describe in the comments the syntax of used functions. The library seems to support four font sizes. The size 1 is hard to read, using the size 2 there are 20 characters per line and must be spaced at least 20 pixels. So you can create a display with 16 rows x 20 columns. By comparing the list with the previous, we note that the 0,0 is the top left.
Since the data are updated approximately every second, you have to delete the pixels written in the previous loop. So as not to rewrite the permanent written, it was decided to cancel the measures with a filled rectangle (FillRectangle), black, large enough to cover the writing. The writings of the measures are of size 3 with a pitch of 40 pixels.

Example#2:  graphics of analog data samples.
Before writing this program I have studied the graphic functions library. In Appendix I listed the main functions.
You can not even call digital oscilloscope the proposed system, but is a good starting point and can already be useful in many applications. The limits are evident: the relative slowness of Arduino (Uno) and writing on a display interfaced with a low speed serial bus.
Note that you must delete the previous point before drawing the current one. To do this I have created two buffers, one for the measures to be graphed and one for the previously tracks, to be outlined in black. You could also reset all memory or draw a black rectangle. The following figure shows the representation of a sinusoidal signal of about 1 Hz.
/* Program TFT_oscillo
 Using the ILI9341 library
 Graphics the time series of 240 8 bit samples
 G. Carrera 18 gen 2013

#include <stdint.h>
#include <TFTv2.h>
#include <SPI.h>

Unsigned long deltat = 14; //sample period in milliseconds
unsigned int data[240];// data buffer
unsigned int datap[240];// previous values buffer (to be clear)
unsigned long pms = 0;
unsigned int ch0;
unsigned int i = 0;

void setup()
  TFT_BL_ON;// TFT backlight on
  Tft.TFTinit();  // TFT library initialize
  // draw string, (x, y), size: 3, color: yellow
  Tft.drawString("GCar - Oscillo",0,0,2,YELLOW);
  Tft.drawString("GCar - Oscillo",0,0,2,BLACK);// black to clear

void loop()
  unsigned long cms = millis();
  // checks if elapsed deltat milliseconds
  if(cms - pms > deltat) {
    pms = cms;// updates pms
    // converts A0
    ch0 = analogRead(A0);
    data[i]= map(ch0, 0, 1023, 0, 319);
    if (i > 239){
      for (int i=0; i <= 239; i++){
        // graphic of data acquired
        Tft.setPixel(i+10, datap[i], BLACK);//clear the previous dot
        Tft.setPixel(i+10, data[i], CYAN);// print new dot
        datap[i]= data[i];// updates previous data
      i = 0;

APPENDIX: library  TFTv2 functions

The following was obtained by analyzing the library files and the data-sheet controller ILI9341. It describes only the main functions.


Display resolution: 240 × 320, with X = 0 to 239 and Y = 0 to 319. The position 0,0 is the top left. The controller has a graphics memory of 240 × 320 × 18 bits = 172800 bytes. The 18 bit color damage 218 = 262,144 colors, with 6 bits for each basic color (RGB). For increased data transfer rates can be used for 16-bit pixels with 65,536 colors (R: 5-bit G: 6-bit, B: 5-bit). As seen on the type of variable color (INT16U), the library uses 16-bit.

TFT_BL_ON;// Turn on the LED backlight

TFT_BL_OFF; //Turn Off the backlight

Tft.TFTinit (void);// initializes the display

Alphanumeric print functions
Tft.drawChar Print a rough character position pox, Poy, of size size and color fgcolor:
Tft.drawChar (INT8U ascii, INT16U pox, INT16U Poy, INT16U size, INT16U fgcolor);

Tft.drawString Print the string string, the position pox, Poy, of size size and color fgcolor:
Tft.drawString (char * string, INT16U pox, INT16U Poy, INT16U size, INT16U fgcolor);

Tft.drawNumber Print the integer long_num position pox, Poy, of size size and color fgcolor:
Tft.drawNumber (long long_num, INT16U pox, INT16U Poy, INT16U size, INT16U fgcolor);

Tft.drawFloat Print floatNumber number with decimal decimal places, the position pox, Poy, of size size and color fgcolor:
Tft.drawFloat (float floatNumber, INT8U decimal, INT16U pox, INT16U Poy, INT16U size, INT16U fgcolor);

Graphic functions
Tft.drawHorizontalLine draw a horizontal line from the position pox, POY, long length and color color:
Tft.drawHorizontalLine (INT16U pox, INT16U Poy, INT16U length, INT16U color);

Tft.drawVerticalLine draws a vertical line from the position pox, POY, long length and color color:
Tft.drawVerticalLine (INT16U pox, INT16U Poy, INT16U length, INT16U color);

Tft.drawLine draw a horizontal line from the position x0, y0 the position x1, y1, color color:
Tft.drawLine (INT16U x0, y0 INT16U, INT16U x1, y1 INT16U, INT16U color);

Tft.drawRectangle draws a rectangle on the position pox, POY, long length, wide width and color color:
Tft.drawRectangle (INT16U pox, INT16U Poy, INT16U length, width INT16U, INT16U color);

Tft.fillRectangle draws a rectangle filled the position pox, POY, long length, wide width and color color:
Tft.fillRectangle (INT16U pox, INT16U Poy, INT16U length, width INT16U, INT16U color);

Tft.drawCircle trace a circle of center pox, Poy, of radius r color color:
Tft.drawCircle (POX int, int Poy, int r, INT16U color);

Tft.drawTraingle trace a triangle vertices poX1, poY1, poX2, poY2, poX3, poY3 and color color:
Tft.drawTraingle (poX1 int, int poY1, poX2 int, int poY2, poX3 int, int poY3, INT16U color);

Tft.setPixel track a point to position pox, Poy and color color:
Tft.setPixel (INT16U pox, INT16U Poy, INT16U color);

Tft.setXY is positioned to point pox, Poy:
Tft.setXY (INT16U pox, INT16U Poy);

How to use the TFT display 2.2" QVGA with Arduino, Part 1/2

Arduino applications with ILI9341 library (Part 1/2)

By Giovanni Carrera 
The main purpose of this project is to build a system based on chip ATMEGA328P, Arduino compatible, and interface it with a 2.2" QVGA TFT display using the library ILI9341. As an alternative to the self-build system you can use an Arduino Pro 3.3 V/ 8 MHz board or similar.
This system works well and can be very useful for many applications, as data-loggers, graphic terminal, etc. A great advantage of this system is to operate at 3.3 V with a modest power (180 mW). It is very suitable to be powered by a lithium 3.6V rechargeable battery. The regulator adopted still works well with a typical drop-out of about 50mV at low currents.
The display
After buying a TFT graphic display for few euros I decided to use it with Arduino. As often happens, the device was not at all documented by the seller neither by the manufacturer. However, the most salient features were screen-printed on the board. The graphics resolution was very good, QVGA means ¼ VGA, i.e. half of the pixels on each side, then 240x320 points to 64K or 256K colors. The interface used is the SPI. On the back of the card it is also a connector for SD card, very suitable to load images or save data. After searching on various forums and sellers in the internet, not without some difficulty, I were able to locate the object, finding a very synthetic scheme, a data sheet of the display and its controller  ILI9341 of Ilitek. By forum came out the link that allowed to download the library Seeed ILI9341 2.2 TFT + SD by Albert Miao, Loovee and Visweswara ( The next photo shows the look of the TFT display used.
The first thing to do was to find out the power supply module, indicated by the abbreviation Vcc. I had to supply the module to 5 or 3.3V? Not even the scheme was to help. On it was indicated only the output voltage of the regulator: 3V, typical supply voltage of the display and the controller, as is apparent from the respective data sheet. On the card was a smd regulator whose name was different from that of the diagram and I did not find its data sheet in the internet. Surely it had to be a low-dropout regulator. I then, decided to supply the module with a voltage of 3.3V as a voltage drop of 0.3V is usually well tolerated by regulators with low drop-out, for currents of relatively modest also the use of 5V could have overheat, if not destroy, the tiny controller.
Having already experienced the self-construction on the bread boards using the Arduino chip ATMEGA328P programmed with boot-loader, I decided for a version  powered to 3.3V instead of the classic 5 V. This is possible because the chip ATmega328P works also at lower voltages (1.8V), and has a less consumption. But the quartz in this case should be 8 MHz instead of 16, I have tried to overclock to 16 without having problems. The 3.3 V power supply simplifies interfacing of the display with the CPU, which otherwise require level converters. I programmed the chip with the Arduino bootloader, but it is easy to buy the chips already programmed. To load the sketch I used a USB serial converter module connected to a PC. The following figure shows the prototype and component layout.

To show the details of the card the display module was removed, it is connected to the board via a 9-pin strip connector (TFT interface) and a 4-wire cable and connector (SD interface). The display is fixed to the board by means of two nylon spacers. On the right it can be see the module that provides to convert the signals of the UART to USB. Below you can see the complete scheme of my system.

You can see the three filters RC low-pass anti-aliasing, with values ​​below the pulsation of cut is equal to:  w=1/(RC),  which corresponds to a frequency of about 340Hz. The three diodes act as protection for voltages greater than 3.3V. The low dropout regulator used is a LM3940-3.3, but you can replace it with an equivalent. The current consumption with display on and is about 50mA.
The image below shows the prototype in operation and with the sketch of test loads, which displays the measures of three analog channels.
When using the Arduino IDE to compile the sketch and send it to the system, it must indicate that the board used (Tools menu) is a "Arduino Pro" clocked by 16MHz and ATmega328 processor. This is a card without USB interface very similar to that proposed.
As showed in the scheme, the first three analog channels are wired to the terminal blocks because I want  to use the prototype to show these measurements on the display and store the data on an SD card.

Serial programming.
This little board is used only to program the card. The module serial / USB should work with the signals Tx, Rx and DTR as well as provide the 5V to our regulator. Only for programming you have to disconnect the external power source using the jumper W1 (P position). DTR is used to give the reset. Of course you must know what chip this board uses and load its driver for your Windows. I used the module Pololu USB01A, with the chip CP2101, but you can use similar modules. The following figure shows the wiring between the module and the connector J1, always made with a small board breadboard.

It is obvious that the Tx signal of this module should be connected to the Rx of the Arduino as well as Rx should be linked to the corresponding Tx, as you do with a null modem serial.

TFT wirings.
The module has a 9-pin 0.1 " strip connector already welded. with the comb already welded. The table shows the connections and the pin assignments for Arduino I / O . Continues in Part 2
TFT board
J2  Pin
Pin I/O
Vcc (+3.3V)