INPUT DEVICES


Home About me

ASSIGNMENT


Measure something: add a sensor to a microcontroller board that you have designed and read it

Board Design


This week I re-designed the board I did in output devices (i.e. week10) with a new board based on both, the Satshakit and Gianluca Pugliese's board.

further details can be found in my final project electronic design page.

eagle

Board has ftdi and ICSP connections, 16 MHz crystal, reset button, extra SDA/SCL pins, 4 VCC and 4 GND pins and two A4988 carriers.

Burning the bootloader:Use the following steps to burn the bootloader using an arduino as ISP.

 1 - connect the arduino uno to the pc
 2 - select the right port and the arduino uno board under tools
 3 - under file->examples find and open the arduino as isp sketch
 4 - upload the sketch to the arduino
 5 - disconnect the arduino from the pc
 6 - connect the board with the arduino by matching pins: mosi-mosi, miso-miso, sck-sck, reset-reset, vcc-vcc, gnd-gnd.
 7 - triple check the connections
 8 - connect the arduino to the pc
 9 - select the right board/processor/frequency -> In this case Arduino Genuino/UNO 
 10 - under tools select the arduino as isp programmer
 11 - double check all the paramters
 12 - click to tools-> burn bootloader 
  

The board is connected to the arduino using the following schematic:

As this board is also recognised as an arduino uno, it can now be programmed using an FTDI cable through the rx/tx pins.

/home/jc/Program Files/arduino-1.6.13/hardware/tools/avr/bin/avrdude -C/home/jc/Program Files/arduino-1.6.13/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -cstk500v1 -P/dev/ttyACM3 -b19200 -e -Ulock:w:0x3F:m -Uefuse:w:0x05:m -Uhfuse:w:0xDE:m -Ulfuse:w:0xFF:m 

avrdude: Version 6.3, compiled on Sep 12 2016 at 15:21:49
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/jc/Program Files/arduino-1.6.13/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/home/jc/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyACM3
         Using Programmer              : stk500v1
         Overriding Baud Rate          : 19200
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500 Version 1.x firmware
         Hardware Version: 2
         Firmware Version: 1.18
         Topcard         : Unknown
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: erasing chip
avrdude: reading input file "0x3F"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.01s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x3F:
avrdude: load data lock data from input file 0x3F:
avrdude: input file 0x3F contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.01s

avrdude: verifying ...
avrdude: 1 bytes of lock verified
avrdude: reading input file "0x05"
avrdude: writing efuse (1 bytes):

Writing |  ***failed;  
################################################## | 100% 0.07s

avrdude: 1 bytes of efuse written
avrdude: verifying efuse memory against 0x05:
avrdude: load data efuse data from input file 0x05:
avrdude: input file 0x05 contains 1 bytes
avrdude: reading on-chip efuse data:

/home/jc/Program Files/arduino-1.6.13/hardware/tools/avr/bin/avrdude -C/home/jc/Program Files/arduino-1.6.13/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -cstk500v1 -P/dev/ttyACM3 -b19200 -Uflash:w:/home/jc/Program Files/arduino-1.6.13/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex:i -Ulock:w:0x0F:m 

avrdude: Version 6.3, compiled on Sep 12 2016 at 15:21:49
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/jc/Program Files/arduino-1.6.13/hardware/tools/avr/etc/avrdude.conf"
Reading | ################################################## | 100% 0.01s

avrdude: verifying ...
avrdude: WARNING: invalid value for unused bits in fuse "efuse", should be set to 1 according to datasheet
This behaviour is deprecated and will result in an error in future version
You probably want to use 0xfd instead of 0x05 (double check with your datasheet first).
avrdude: 1 bytes of efuse verified
avrdude: reading input file "0xDE"
avrdude: writing hfuse (1 bytes):

Writing | ################################################## | 100% 0.02s

avrdude: 1 bytes of hfuse written
avrdude: verifying hfuse memory against 0xDE:
avrdude: load data hfuse data from input file 0xDE:
avrdude: input file 0xDE contains 1 bytes
avrdude: reading on-chip hfuse data:

Reading | ################################################## | 100% 0.01s

avrdude: verifying ...
avrdude: 1 bytes of hfuse verified
avrdude: reading input file "0xFF"
avrdude: writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.02s

avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0xFF:
avrdude: load data lfuse data from input file 0xFF:
avrdude: input file 0xFF contains 1 bytes
avrdude: reading on-chip lfuse data:

Reading | ################################################## | 100% 0.01s

avrdude: verifying ...
avrdude: 1 bytes of lfuse verified

avrdude done.  Thank you.

         User configuration file is "/home/jc/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyACM3
         Using Programmer              : stk500v1
         Overriding Baud Rate          : 19200
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500 Version 1.x firmware
         Hardware Version: 2
         Firmware Version: 1.18
         Topcard         : Unknown
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "/home/jc/Program Files/arduino-1.6.13/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex"
avrdude: writing flash (32768 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against /home/jc/Program Files/arduino-1.6.13/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex:
avrdude: load data flash data from input file /home/jc/Program Files/arduino-1.6.13/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex:
avrdude: input file /home/jc/Program Files/arduino-1.6.13/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex contains 32768 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 32768 bytes of flash verified
avrdude: reading input file "0x0F"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.02s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x0F:
avrdude: load data lock data from input file 0x0F:
avrdude: input file 0x0F contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.01s

avrdude: verifying ...
avrdude: 1 bytes of lock verified

avrdude done.  Thank you.
 
  

Now that my board is ready, I tested several input devices.

ADXL335 ACCELEROMETER


The datasheet of the device is available here.

The adxl335 is a 3-axis accelerometer with signal-conditioned voltage outputs. The range of measurement is of +-3g.

Bandwidths of the accelerometer can be selected with a range of 0.5Hz to 1600Hz for XY axes and 0.5Hz to 550Hz for Z axis.

Output signals are analog voltages proportional to the static acceleration of gravity.

As signals are analog, analog pins must be used.

Pin connections:

|---------------+---------------------------------------+---------|
| accelerometer | board                                 | arduino |
|---------------+---------------------------------------+---------|
| vcc           | step-down voltage regulator (3v)->vcc | vcc 3V  |
| gnd           | gnd                                   | gnd     |
| x-out         | pc0                                   | a0      |
| y-out         | pc1                                   | a1      |
| z-out         | pc2                                   | a2      |
|---------------+---------------------------------------+---------|
  

Each axis of the accelerometer can only differentiate 180 degrees of movement.

A simple code is written to read the value of the accelerometer:

const int xpin = PC0; // x-axis of the accelerometer
const int ypin = PC1; // y-axis
const int zpin = PC2; // z-axis (only on 3-axis models)

void setup()
{
  // initialize the serial communications:
  Serial.begin(9600);
}
void loop()
{
  int x = analogRead(xpin); //read from xpin
  delay(1); //
  int y = analogRead(ypin); //read from ypin
  delay(1); 
  int z = analogRead(zpin); //read from zpin

  Serial.print(x); 
  Serial.print("\t");
  Serial.print(y);
  Serial.print("\t");
  Serial.print(z); 
  Serial.print("\n");
  delay(1000);
} 
  

I obtained the following output from GtkTerm:

339     329    273
  

These values can be used as a base to detect movement, with the exception of Yaw rotation, which will require a gyro too.

BMP180 PRESSURE SENSOR


Datasheet is available . The BMP180 is an I2C interface pressure sensor supporting a pressure range between 300hPa to 1100hPa (or 9000m to -500m, above or below the sea level, respectively).

The BMP180 is based on piezo resistive technology with a resolution of 0.01hPa and 0.1 C.

Sampling accuracy can be set via software with the variable oversampling_setting, modes available are:

pressure

BMP180 can be easily interfaced with a microcontroller through sda/scl pins.

Pin connections:

|------------------+-------+-------------|
| barometer sensor | board | arduino uno |
|------------------+-------+-------------|
| vin              | vcc   | vcc         |
| gnd              | gnd   | gnd         |
| sda              | PC4   | a4          |
| scl              | PC5   | a5          |
|------------------+-------+-------------|
  

Altitude in meters can be calculated using the international barometric formula:

$$ \text{Altitude} = 44330 * \Bigg(1 - (\frac{p}{p_0})^\frac{1}{5.255}\Bigg) $$

A small program was written using the following library:

Adafruit BMP Library

Program prints in serial the pressure, temperature and estimated altitude.

For the altitude measurements; as recommended by the library documentation, it is necessary to include the location's mean sea level pressure. In my case for Kleve, it is 1011hPa.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
/* Kleve mean sea level pressure in hPa*/
#define KLEVE_SLP 1011

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

void setup(void) 
{
  Serial.begin(9600);
  Serial.println("Pressure Sensor Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!bmp.begin())
  {
    Serial.print("no BMP085 detected");
    while(1);
  }
}

/**************************************************************************/
/*
    Loop function
*/
/**************************************************************************/
void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event;
  bmp.getEvent(&event);
 
  /* barometric pressure in hPa) */
  if (event.pressure)
  {
    /* Display atmospheric pressue in hPa */
    Serial.print("Pressure:    ");
    Serial.print(event.pressure);
    Serial.println(" hPa");
    
    float temperature;
    bmp.getTemperature(\&temperature);
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" C");

    /* Then convert the atmospheric pressure, and SLP to altitude         */
    float seaLevelPressure = KLEVE_SLP;
    Serial.print("Estimated Altitude:    "); 
    Serial.print(bmp.pressureToAltitude(seaLevelPressure,
                                        event.pressure)); 
    Serial.println(" m");
    Serial.println("");
    delay(2000);
  }
  else
  {
    Serial.println("Sensor error");
  }
  delay(1000);
}
  

Configure GtkTerm with the right port and baud rate:

The output on the screen will be updated every 2 seconds:

DHT11 HUMIDITY SENSOR


Datasheet can be found here.

The following library is used:

DHT sensor library

Similarly to the pressure sensor, data can be received via serial.

Pin connections:

|-----------------+-------+-------------|
| humidity sensor | board | arduino uno |
|-----------------+-------+-------------|
| sig             | pd7   | d7          |
| vcc             | vcc   | vcc         |
| gnc             | gnd   | gnd         |
|-----------------+-------+-------------|
  

Program sends to serial measurements at 2sec intervals. DHT11 and pin must be defined before taking measurements.

#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN            PD7
#define DHTTYPE           DHT11 

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
  Serial.begin(9600); 
  // Initialize device.
  dht.begin();
  Serial.println("DHT11 test");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
}

void loop() {
  // Delay between measurements.
  delay(2000);
  // Get temperature event and print its value.
  sensors_event_t event;  
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println("Error reading temperature!");
  }
  else {
    Serial.print("Temperature: ");
    Serial.print(event.temperature);
    Serial.println(" C");
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println("Error reading humidity!");
  }
  else {
    Serial.print("Humidity: ");
    Serial.print(event.relative_humidity);
    Serial.println("%");
  }
}
  

Output of the program is:

LESSONS LEARNED


FILES


jc-watchobot-v1.0 eagle files

ADXL335 test code

Humidity sensor test code

Pressure sensor test code


The content of this page is licensed under Attribution-NonCommercial 4.0 International CC BY-NC 4.0. Any source code, if otherwise unspecified, is licensed under the MIT License