sábado, 8 de noviembre de 2014

Accelerometer with LCD and SD



Arduino - Accelerometer with LCD & SD

This project consist in the measurement of acceleration in 3 axis (x-y-z) with an ArduinoUno as a microcontroler. The accelerometer used was the adxl335. Results of the measurements are shown on a LCD screen (Adafruit 1.8" Color TFT Shield w/microSD and Joystick). The Data obtained is saved on a SD memory card, included on the LCD screen. The Data is save on the microSD by creating a data.txt file. The shield's Joystick allow the program to pause (moving the joystick down) and run again (moving the joystick up).




      Materials:

  1. Arduino Uno (from Arduino store)
  2. adxl335 sensor (from sparkfun)
  3. Adafruit 1.8" Color TFT Shield w/microSD and Joystick (from adafruit)

Assembly and wiring:

  1. Assembly of the 1.8" TFT Shield  (detailed assembly process can be obtain from adafruit)
  2. Connect 1.8" TFT Shield to Arduino 
  3. Connect adxl335 to the 1.8" TFT Shield  (A3 cannot be used because it is already be in used by the shield Joystick)
    1. Connect the VCC to A5.
    2. Connect the GND to A4.
    3. Connect "X" to A2
    4. Connect "Y" to A1
    5. Connect "Z" to A0

Libraries:

For this project to be able to run properly it must have two aditional libraries and posibly a library actualization. A guide for installing libraries can be obtain from Arduino (Installing Additional Arduino Libraries). The libraries are:
If the library is installed right it can be seen the codes in the examples, as shown in the picture.

 

The adafruit ST7735 examples allow to learn how to use the LCD display of the TFT shield.

Code


#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SD.h>
#include <SPI.h>

#if defined(__SAM3X8E__)
    #undef __FlashStringHelper::F(string_literal)
    #define F(string_literal) string_literal
#endif

// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins.  For Arduino Uno,
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
#define SD_CS    4  // Chip select line for SD card
#define TFT_CS  10  // Chip select line for TFT display
#define TFT_DC   8  // Data/command line for TFT
#define TFT_RST  -1  // Reset line for TFT (or connect to +5V)

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
#define BUTTON_NONE 0
#define BUTTON_DOWN 1
#define BUTTON_RIGHT 2
#define BUTTON_SELECT 3
#define BUTTON_UP 4
#define BUTTON_LEFT 5

//Analog read pins
const int groundpin = 18;
const int powerpin = 19;
const int xPin = A2;
const int yPin = A1;
const int zPin = A0;
boolean run = false;

File dataFile;
const int chipSelect = 4;

void setup(){
  Serial.begin(9600);
//analog to gnd & 5v
pinMode(groundpin,OUTPUT);
pinMode(powerpin,OUTPUT);
digitalWrite(groundpin,LOW);
digitalWrite(powerpin,HIGH);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(zPin, INPUT);
  // Initialize 1.8" TFT
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
  Serial.println("OK!");
  tft.fillScreen(ST7735_BLACK);

Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(SS, OUTPUT);
  
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  if (SD.exists("data.txt")){
    SD.remove("data.txt");
  }
  dataFile = SD.open("data.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
}
 
uint8_t readButton(void) {
  float a = analogRead(3);
 
  a *= 5.0;
  a /= 1024.0;
 
  //Serial.print("Button read analog = ");
  //Serial.println(a);
  if (a < 0.2) return BUTTON_DOWN;
  if (a < 1.0) return BUTTON_RIGHT;
  if (a < 1.5) return BUTTON_SELECT;
  if (a < 2.0) return BUTTON_UP;
  if (a < 3.2) return BUTTON_LEFT;
  else return BUTTON_NONE;
}


void loop() {
  uint8_t b = readButton();
  if (b == BUTTON_UP) {
    //if (run == false){
      run = true;
      // create new file of data, if it already exist it will be deleted
      // and create new data file
      if (SD.exists("data.txt")){
        SD.remove("data.txt");
        dataFile = SD.open("data.txt", FILE_WRITE);
      }
      tft.setTextSize(2);
      tft.setTextColor(ST7735_BLACK);
      tft.setCursor(0, 120);
      tft.print("Pause");
      tft.setTextColor(ST7735_MAGENTA);
      tft.setCursor(0, 120);
      tft.print("Running"); 
    }
   
  if (b == BUTTON_DOWN) {
      run = false;
      tft.setTextColor(ST7735_BLACK);
      tft.setCursor(0, 120);
      tft.print("Running"); 
      tft.setTextColor(ST7735_MAGENTA);
      tft.setCursor(0, 120);
      tft.print("Pause");      
    }
   
  if (run == true) {
  //read the analog values from the accelerometer
  int x = analogRead(xPin);
  int y = analogRead(yPin);
  int z = analogRead(zPin);
  float zero_G = 512.0;
  float scale = 102.3;
  double xnw = ((float)x - zero_G)/scale;
  double ynw = ((float)y - zero_G)/scale;
  double znw = ((float)z - zero_G)/scale;
 
  Serial.print(xnw);
  Serial.print("\t");
  Serial.print(ynw);
  Serial.print("\t");
  Serial.print(znw);
  Serial.print("\n");
 
  //print on sd
  dataFile.print(xnw);
  dataFile.print("\t");
  dataFile.print(ynw);
  dataFile.print("\t");
  dataFile.print(znw);
  dataFile.print("\n");
 
  // print on screen
  tft.setTextSize(2);
  tft.setTextColor(ST7735_BLUE);
    tft.setCursor(0, 10);
    tft.print("x = ");
    tft.setCursor(50, 10);
    tft.print(xnw);
  tft.setTextColor(ST7735_RED);
    tft.setCursor(0, 40);
    tft.print("y = ");
    tft.setCursor(50, 40);
    tft.print(ynw);
  tft.setTextColor(ST7735_GREEN);
    tft.setCursor(0, 70);
    tft.print("z = ");
    tft.setCursor(50, 70);
    tft.print(znw);
   
  dataFile.flush();

  delay(100);//just here to slow down the serial output - Easier to read
 
    tft.setTextColor(ST7735_BLACK);
    tft.setCursor(50, 10);
    tft.print(xnw);
    tft.setCursor(50, 40);
    tft.print(ynw);
    tft.setCursor(50, 70);
    tft.print(znw);
  }
}
   

SD file

Plus the results shown in the LCD, seen in the first picture, the date is save inside the SD card and be used in your computer.


 



Arduino - Accelerometro con LCD y SD


 Este projecto consiste  el la medición de la acceleración en los tres differentes ejes(x-y-z) utilizando un arduino como microcontrolador. El accelerometro utilizado es el ADXL335. Los resultados de las mediciones se muestran es una pantalla LCD (Adafruit  1.8" Color TFT Shield w/ micro SD and Joystick). La data obtenida es salvada en la tarjeta de memoria(microSD) creando un documento data.text. La palanca de mando del TFT Shield permite que el programa haga pausas (moviendo la palanca de mando hacia abajo) y corriendo de nuevo (moviendo la palanca de mando hacia arriba).



Materiales:

     1. Arduino Uno (de arduino store)
     2. Adxl 335 (de sparkfun)
     3. Adafruit  1.8" Color TFT Shield w/ micro SD and Joystick( de adafruit)

Ensamblaje y Cableado:

 1. Ensamble 1.8" TFT Shield (el proceso detallado puede ser encotrado en adafruit).
 2. Conecte el 1.8'' TFT Shield al arduino.
 3. Conecte el adxl 335 al 1.8'' TFT Shield (A3 no puede ser usado porque ya esta en uso con la palanca de mando)

    1. Conecte el VCC a 5V.
    2. Conecte el GND a A4.
    3. Conecte "X'' a A2.
    4. Conecte ''Y"a A1.
    5. Conecte "Z'' a A0.


   Líbrerias:

Para que este proyecto sea capaz de ejecutarse correctamente debe tener dos bibliotecas adicionales y posiblemente una actualización de biblioteca. Una guía para la instalación de las bibliotecas se puede obtener de Arduino (Instalación Adicional de Librerías de Arduino) Las librerías son:



 Si se instalan la librerías los ejemplos pueden ser observados como se muestran en la siguiente figura.
   
   

Los ejemplos ST7735 adafruit permiten aprender cómo utilizar la pantalla LCD de la pantalla TFT.

 Código:

#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SD.h>
#include <SPI.h>

#if defined(__SAM3X8E__)
    #undef __FlashStringHelper::F(string_literal)
    #define F(string_literal) string_literal
#endif

// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins.  For Arduino Uno,
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
#define SD_CS    4  // Chip select line for SD card
#define TFT_CS  10  // Chip select line for TFT display
#define TFT_DC   8  // Data/command line for TFT
#define TFT_RST  -1  // Reset line for TFT (or connect to +5V)

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
#define BUTTON_NONE 0
#define BUTTON_DOWN 1
#define BUTTON_RIGHT 2
#define BUTTON_SELECT 3
#define BUTTON_UP 4
#define BUTTON_LEFT 5

//Analog read pins
const int groundpin = 18;
const int powerpin = 19;
const int xPin = A2;
const int yPin = A1;
const int zPin = A0;
boolean run = false;

File dataFile;
const int chipSelect = 4;

void setup(){
  Serial.begin(9600);
//analog to gnd & 5v
pinMode(groundpin,OUTPUT);
pinMode(powerpin,OUTPUT);
digitalWrite(groundpin,LOW);
digitalWrite(powerpin,HIGH);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(zPin, INPUT);
  // Initialize 1.8" TFT
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
  Serial.println("OK!");
  tft.fillScreen(ST7735_BLACK);

Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(SS, OUTPUT);
  
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  if (SD.exists("data.txt")){
    SD.remove("data.txt");
  }
  dataFile = SD.open("data.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
}

uint8_t readButton(void) {
  float a = analogRead(3);

  a *= 5.0;
  a /= 1024.0;

  //Serial.print("Button read analog = ");
  //Serial.println(a);
  if (a < 0.2) return BUTTON_DOWN;
  if (a < 1.0) return BUTTON_RIGHT;
  if (a < 1.5) return BUTTON_SELECT;
  if (a < 2.0) return BUTTON_UP;
  if (a < 3.2) return BUTTON_LEFT;
  else return BUTTON_NONE;
}


void loop() {
  uint8_t b = readButton();
  if (b == BUTTON_UP) {
    //if (run == false){
      run = true;
      // create new file of data, if it already exist it will be deleted
      // and create new data file
      if (SD.exists("data.txt")){
        SD.remove("data.txt");
        dataFile = SD.open("data.txt", FILE_WRITE);
      }
      tft.setTextSize(2);
      tft.setTextColor(ST7735_BLACK);
      tft.setCursor(0, 120);
      tft.print("Pause");
      tft.setTextColor(ST7735_MAGENTA);
      tft.setCursor(0, 120);
      tft.print("Running"); 
    }
   
  if (b == BUTTON_DOWN) {
      run = false;
      tft.setTextColor(ST7735_BLACK);
      tft.setCursor(0, 120);
      tft.print("Running"); 
      tft.setTextColor(ST7735_MAGENTA);
      tft.setCursor(0, 120);
      tft.print("Pause");      
    }
   
  if (run == true) {
  //read the analog values from the accelerometer
  int x = analogRead(xPin);
  int y = analogRead(yPin);
  int z = analogRead(zPin);
  float zero_G = 512.0;
  float scale = 102.3;
  double xnw = ((float)x - zero_G)/scale;
  double ynw = ((float)y - zero_G)/scale;
  double znw = ((float)z - zero_G)/scale;

  Serial.print(xnw);
  Serial.print("\t");
  Serial.print(ynw);
  Serial.print("\t");
  Serial.print(znw);
  Serial.print("\n");

  //print on sd
  dataFile.print(xnw);
  dataFile.print("\t");
  dataFile.print(ynw);
  dataFile.print("\t");
  dataFile.print(znw);
  dataFile.print("\n");

  // print on screen
  tft.setTextSize(2);
  tft.setTextColor(ST7735_BLUE);
    tft.setCursor(0, 10);
    tft.print("x = ");
    tft.setCursor(50, 10);
    tft.print(xnw);
  tft.setTextColor(ST7735_RED);
    tft.setCursor(0, 40);
    tft.print("y = ");
    tft.setCursor(50, 40);
    tft.print(ynw);
  tft.setTextColor(ST7735_GREEN);
    tft.setCursor(0, 70);
    tft.print("z = ");
    tft.setCursor(50, 70);
    tft.print(znw);
   
  dataFile.flush();

  delay(100);//just here to slow down the serial output - Easier to read

    tft.setTextColor(ST7735_BLACK);
    tft.setCursor(50, 10);
    tft.print(xnw);
    tft.setCursor(50, 40);
    tft.print(ynw);
    tft.setCursor(50, 70);
    tft.print(znw);
  }
}

Documento en SD

Además de los resultados mostrados en la LCD, vistos en la figura mostrada al inicio, la data se guarda en el SD y puede ser visto desde su computadora.





No hay comentarios:

Publicar un comentario