AdSense

Sunday 14 December 2014

Arduino - Read out height sensor GY-65

(Deutsche Version) A common way to measure relative heights is the usage of a barometric height sensor. Such a sensor measures the pressure and by using the barometric formula (Wikipedia), one can calculate the height. The ambient pressure varies during the days therefore the sensor is calibrated at a well-known height and the other heights are calculated by using this reference pressure and height. The measured heights have a precision of up to 1 meter.

If  you look for the GY-65 at ebay, you can find the sensor for about 5 euro. The sensor has a usual I2C interface so only two wires are needed. Additionally, the I2Cdev library is needed which can be downloaded here. The source code is as simple as possible, at first the sensor is initialised, then measuring the pressure is set and afterwards the pressure is read out and calculated into the height. A basic program looks like this:


#include <I2Cdev.h>
#include "BMP085.h"
#include <Wire.h>

BMP085 barometer;

double pressure;
double altitude;


void setup() {
  Wire.begin();
  
  Serial.begin(9600);
  Serial.println("starting...");
    
  //initialize the barometer
  barometer.initialize();
}

void loop()
{
  // request pressure (3x oversampling mode, high detail, 23.5ms delay).
  // Let's just wait a bit more to be sure...
  barometer.setControl(BMP085_MODE_PRESSURE_3);
  delay(30);
  // read calibrated pressure value in Pascals (Pa)
  pressure = barometer.getPressure();
  // calculate absolute altitude in meters based on known pressure
  // (may pass a second "sea level pressure" parameter here,
  // otherwise uses the standard value of 101325 Pa)
  altitude = barometer.getAltitude(pressure); 
  
  // print back the calculated altitude
  Serial.println(altitude);
  // do this every second
  delay(1000);
}

I2C OLED display

(Deutsche Version) If you look for "Arduino OLED Display" on Ebay, you will find a display with a size of 1 inch and a resolution of 128x84 pixels which is controlled via I2C. It costs less than 4 Euros. Of course, 1 inch isn't much, but I was curious and so I ordered it. To show you how small it really is, I placed a coin next to it:


Connecting the display is very easy (thanks to I2C): you only need the voltage supply and two wire for I2C. But how can you control the display? After some searching on Google I found out that the display is a clonde of the Adafruit OLED display called SSD1306. Adafruit offers an Arduino library for this display which can be downloaded HERE. Additionally you need the Adafruit-GFX-Library. After installing the two libraries you will find the example "ssd1306_128x64_i2c" under "Adafruit_SSD1306" in the Arduino IDE. But if you transmit this example to the Arduino nothing will happen. The china-clone-display has a different I2C adress than the original Adafruit display. You will have to change something in the setup() function. Change
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
to
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64).

After that you can transmit the example to your Arduino and you should see a small demonstration of the different functions of the GFX library on the display. Since there are a lot of functions and the example has a lot of code I will not explain it in detail. I think all of the functions in the example are self-explanatory. I you have problems understanding the code, just have a look in the header file of the GFX library. There you find further explanations like parameters for the functions. You can print text, draw squares, triangles, etc. Even inverting or scrolling the screen is possible.

You also have the possibility to print a user-defined bitmap. I'm going to explain this:

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define XPOS 50
#define YPOS 25
#define BITMAP_HEIGHT 16 
#define BITMAP_WIDTH  16 
static const unsigned char PROGMEM bitmap[] =
{ B00000001, B10000000,
  B00000010, B01000000,
  B00000100, B00100000,
  B00001000, B00010000,
  B00010000, B00001000,
  B00111111, B11111100,
  B00101000, B00010100,
  B00100100, B00100100,
  B00100010, B01000100,
  B00100001, B10000100,
  B00100010, B01000100,
  B00100100, B00100100,
  B00101000, B00010100,
  B00110000, B00001100,
  B00111111, B11111100,
  B00000000, B00000000 };

void setup()   {                

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)
  
  //clear display buffer
  display.clearDisplay();
  //draw a bitmap stored in variable bitmap with given size at XPOS, YPOS in color white
  display.drawBitmap(XPOS, YPOS, bitmap, BITMAP_WIDTH, BITMAP_HEIGHT, WHITE);
  display.display();
 
}


void loop() { 
}


In this example we are going to draw a bitmap with the size of 16x16 pixels. Of course you can choose another size. The bitmap has to be stored in a character array. It is very important to use the keyword "PROGMEM" when declaring the variable. This will make the Arduino store the variable in the flash memory. The library only accepts bitmaps stored in flash. In the example, two characters store the 16 bit data of a line of the bitmap. If a bit is set, a pixel is set when the bitmap is drawn. To increase readability, line breaks were inserted. On line in the code is on line of the bitmap. To draw the bitmap, you have to call the function drawBitmap(). Parameters are: x- and y-position of the bitmap, the variable in which the bitmap is stored, height und width of the bitmap and its color (BLACK or WHITE) of a set pixel.

The result:

Friday 5 December 2014

Sharpen a brush

(Deutsche Version) A newly bought brush is usually sharp and also a bit hard so you can draw very fine lines. After a while, the hair of the brush starts to point into every direction so you cannot use the brush any more. To get the brush back into the state which it has been after buying, you can use a simple trick.
A used brush which you cannot use for fine details any more.
First you have to create a mixture of sugar and water. Simply mix the two ingredients and stir. Now you may put the brush in the liquid. The surface tension keeps the hair together.
Brush tip with the sugar water mixture applied. The hair is kept together (Surface tension).
To get the brush back into the perfect shape, you have to wipe off the brush off carefully, this gets it even sharper. If the brush dries, the tip gets even better, as good as for a newly bought brush. I use brushes of the size 00, you can get them in every toy's shop. If you take good care of the brush, you should be able to use it forever.
The sharpened brush.