Sunday, 11 September 2016

Read DHT11 sensor with Arduino

(Deutsche Version) Some time ago, I bought a temperature and humidity sensor which is called DHT11. The sensor is easy to use and can be used with 3.3V or 5V. The sensor has four pins, for the normal operation, only three of them are needed.
The left pin (as you can see in the image) is connected to 5V, the pin on the right with GND. The left pin of the middle pins is the signal pin which can be connected with a random digital pin on the arduino (in my example, this is pin 2). Additionally, a 10k resistor is needed between the data pin and 5V.

Next, you need the corresponding library which you can download here: Download. In the arduino software the sensor can be read as it is shown in the following example. The source code should be self-explainatory. If a sensor with a higher precision is needed, you can use a DHT22, therefore you just have to adjust the DHTTYPE definition.

#include "DHT.h"
#define DHTPIN 2

#define DHTTYPE DHT11


void setup() {


void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Serial.print("Humidity: ");
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.println(" *C ");


Sunday, 17 July 2016

3D printing profiles for the Ultimaker 2

(Deutsche Version) After a long time with no updates I want to introduce a new topic to this blog: 3D printing. I own an Ultimaker 2 and in this first post, I want to present the printing profiles I use. I only use black PLA from Innofill. This means that I do not have to fine tune the printing profiles if I buy new material because the properties of the material stay the same.

Ultimaker 2:
At the printer, I only set some small values. I have three profiles: Fast, Fine and Ultrafine. For all three of them I use a Heatbed Temperature of 70 °C. This get the PLA to stick to the platform. The Nozzle Temperatures are 210 °C for Fast, 195 °C for Fine and 172 °C for Ultrafine. This last temperature is really close to the melting point, this means that if the temperature in your house changes (summer / winter) you have to adjust this temperature, in the winter I usually have 175 °.

Printing profiles for Cura:
Usually, PLA does not need any cooling by the fans. If you activate this cooling, the print will warp upwards. Therefore the profiles Fast and Fine have a Minimum Fan Speed of 10%, whereas Ultrafine has 60%. This means that small structures are cooled quite good and the print does not melt during the printing process as the nozzle always stays at nearly the same position.
The Layer Height is 0.1 mm for Fast and 0.05 mm for Fine and Ultrafine. The Shell Thickness is 0.8 mm.
The printing speed is set to 25-40 for Fast, 20-25 for Fine and 10-25 for Ultrafine.

If anyone is interested in the profile files, you can download them here:

Wednesday, 23 September 2015

nrf24l01+ with ATtiny84

(Deutsche Version) I already explained how to tun the wireless module nrf24l01+ with an Arduino or ATmega in an earlier post.  If you want to run the module with an ATtinyx4, e.g. the ATTiny84, you have to do some changes.

In this post, I want to explain how to set up a wireless communication between an Arduino and an ATtiny84 using the ping example of the Mirf library. The Arduino sends a ping signal, the ATtiny receives the signal and sends it back.

Before you start, you have to install the ATtiny cores for the Arduino IDE like I explained HERE.

Libraries needed

The wireless module nrf24l01+ uses SPI to communicate with the microcontroller. Unfortunately, the ATtiny has no hardware SPI so you have to use software to emulate the SPI connection. This is done using the so called Universal Serial Interface (USI). As usual there is a library for this, so you don't have to worry about that. Besides that, you need a modified version of the Mirf library. Both libs can be downloaded HERE. After extracting the folders "Mirf" and "SPI85" to the "libraries" folder of the Arduino IDE, you can use these libs.


The wiring is pretty similar to the one in the example with the ATmega. CSN, CE, SCK, MOSI and MISO as well as VCC and GND have to be connected. Don't forget that the module runs with a voltage of 3,3V! The follwoing picture shows how the connect the wireless module to the ATtiny84.
Important: in the datasheet of the ATtiny you will find some pins called MISO, MOSI and SCK. These pins are only for connecting your programmer (e.g. the USBasp). The module has to be connected to USI-DI (corresponds to MISO), USI-DO (MOSI) and USCK (SCK). By default, the pins D4 to D6 are used for these pins. If you want, you can change these pins but you must not set USI-DI to the MISO pin of your programmer. The same applies for USI-DO and MOSI pin.


The original "ping_client" example of the Mirf library runs on the Arduino. For the ATtiny, you have to use a slightly changed version of the "ping_server" example:

#include <SPI85.h>
#include <Mirf.h>
#include <MirfHardwareSpi85Driver.h>

// This USI was defined in SPI85.cpp
// Not to be confused with SPI (MOSI/MISO) used by ICSP pins
// Refer to page 61 of attiny84 datahseet
// USI pins could be redefined here
//#define USI-DO  5
//#define USI-DI  4
//#define USCK   6

#define CE    7    
#define CSN   3 

void setup(){

  /*   * Set the SPI Driver.   */

  Mirf.spi = &MirfHardwareSpi85;
  /*   * Setup pins / SPI.   */
  Mirf.cePin = CE;
  Mirf.csnPin = CSN;
  /*   * Configure reciving address.   */
  Mirf.setRADDR((byte *)"serv1");
  /*   * Set the payload length to sizeof(unsigned long) the   * return type of millis().   *   * NB: payload on client and server must be the same.   */
  Mirf.payload = sizeof(unsigned long);
  /*   * Write channel and payload config then power up reciver.   */


void loop(){
  /*   * A buffer to store the data.   */
  byte data[Mirf.payload];
  /*   * If a packet has been recived.   *   * isSending also restores listening mode when it    * transitions from true to false.   */
  if(!Mirf.isSending() && Mirf.dataReady()){

    /*     * Get load the packet into the buffer.     */
    /*     * Set the send address.     */
    Mirf.setTADDR((byte *)"clie1");
    /*     * Send the data back to the client.     */
    /*     * Wait untill sending has finished     *     * NB: isSending returns the chip to receving after returning true.     */

The only difference to the version for ATmega is that SPI85.h and MirfHardwareSpi85Driver.h are included and MirfHardwareSpi85 is chosen as SPI driver in the setup():
Mirf.spi = &MirfHardwareSpi85;

When you upload the code to the ATtiny using a programmer, the pinging Arduino should get a response from the ATtiny.

I had some problems flashing the ATtiny with the USBasp as soon as the wireless module was connected to the ATtiny. I think this happend because the module and the programmer use the same pins. I have to disconnect the voltage supply of the module while flashing, then everything works fine.

Tuesday, 22 September 2015

ATtiny and the Arduino IDE - RELOADED

(Deutsche Version) Some time ago, I published a post with the topic "ATiny and Arduino" in which I explained how to install the so called ATiny cores for Arduino to be able to program the ATtiny with the Arduino IDE.

Since I needed anohther core for my new projects, I wanted to write a short post about it, too. The core can be used for ATtiny25, ATtiny45, ATtiny85, ATtiny24, ATtiny44 and ATtiny84.

All the files you need can be downloaded HERE. After extracting the archive, copy the folder "tiny" to the folder "hardware" of your Arduino installation (e.g.  "C:\Programs\Arduino\hardware\tiny"). Open the subfolder "avr" in the folder "tiny" and rename the file "Prospective Boards.txt" to "boards.txt".

After restarting the Arduino IDE, you can choose the desired ATtiny. Since I use the german version of the IDE, I'm not sure how the menu is called in the english version. I think it sould be something like "Tools" - "Boards". Have a look at the screenshot if you can't find the right menu. The ATiny can now be programmed with the IDE using a programmer like the USBasp. It is important that the fuses of the ATtiny are set correctly and fit to the chosen settings.

Pinout can be found in the file "pins_arduino.c" in the folder "tiny\avr\cores\tiny".
For the ATtinyx4 it's for example:
//                                        +-\/-+
//                               VCC  1|    |14  GND
//                     (D  0)  PB0  2|    |13  AREF (D 10)
//                     (D  1)  PB1  3|    |12  PA1  (D  9)
//                               PB3  4|    |11  PA2  (D  8)
//  PWM  INT0  (D  2)  PB2  5|    |10  PA3  (D  7)
//    PWM        (D  3)  PA7  6|    |9   PA4  (D  6)
//    PWM        (D  4)  PA6  7|    |8   PA5  (D  5)        PWM
//                                       +----+

Tuesday, 10 February 2015

Serial communication with Matlab or C#

(Deutsche Version) In the last post, I showed how to read out a heartbeat sensor (Read out heartbeat sensor). In this post, I want to explain how to read this data with Matlab or C#. I want to start with Matlab. At first, you have to open a serial port:

s = serial('COM3');

You simply have to plug in the number of your COM port. Afterwards you can read all existing characters:

str = fscanf(s);

Keep in mind that it can also occur that the Arduino has only sent half of its message when the computer is reading all existing characters, and in the next read instruction from the computer, the rest of the message (and possibly further messages) is read in. In the end you should close the port:


Now, I want to explain how to do this in C#. Again, you have to open a port:

port = new SerialPort("COM3", 9600);

Afterwards, you can read in all existing characters (analog to the example with Matlab):

string indata = sp.ReadExisting();

In C#, you don't have to close the port because this happens automatically when closing the program.

Saturday, 24 January 2015

Read out heartbeat sensor

(Deutsche Version) If you look for "heartbeat sensor" on ebay, you find a small circuit board which contains an LED and a photo transistor. This board claims to be able to measure the heart beat. I bought one sensor and tried it. Unfortunately, there is very few (ok, basically absolutely no) documentation about the sensor and not even something like a product key, only xinda and Lcup can be found on the sensor.
The heartbeat sensor
The sensor has 3 pins which are labelled very good. One of them has a -, here you have to connect GND. The next pin has an S, this is the pin for the sensor output. The last pin (in the middle) which has no label is connected to the LED. This pin has to be connected to +5V and the sensor output has to be connected to an analog input of the arduino.

The program reads the sensor value and sends it to the computer via the serial port where you can further process the data (e.g. with Matlab, maybe I will write a post about that). Here is a picture of the plotted heartbeat data on the computer and below the code for the Arduino.
//Define pins for LED and sensor.
int ledPin = 13;
int sensorPin = 0;
//alpha is used in the original proposed code by the company (see below).
double alpha = 0.75;
//lasttime is used to have a very precise measurement of the time so the calculated pulse is correct.
unsigned long lasttime;

void setup ()
  //Switch on the LED.
  pinMode (ledPin, OUTPUT);
  Serial.begin (9600);
  lasttime = micros();

void loop ()
  //Also used for smoothening the signal.
  static double oldValue = 0;
  //Wait 10 ms between each measurement.
  while(micros() - lasttime < 10000)
  //Read the signal.
  int rawValue = analogRead (sensorPin);
  lasttime += 10000;
  //In the "original" code example, "value" was sent to the computer. This calculation basically smoothens "rawValue".
  //double value = alpha * oldValue + (1 - alpha) * rawValue;
  //Send back the measured value.
  Serial.println (rawValue);
  oldValue = value;

Sunday, 18 January 2015

A gaming table for tabletops

(Deutsche Version) For tabletop games you need some kind of base to play on. For the beginning, a small table or simply the floor is sufficient but if you start to have large battles, something bigger is needed. The usual size of tables for 28 mm tabletops is about 6 x 4 feet, this is about 183 x 122 cm. Such a table is quite large and needs a lot of space, it can take up to a whole room. Therefore I created a table which can be set up and dismounted within 2 minutes.

The basic idea is simple. A large plate of wood is layed onto an existing coffee table. My coffee table is about 110 x 70 cm, so a plate of 183 x 122 cm would hang down. Therefore, the gaming table required to have feet. The result was a gaming table consisting of two plates of the size 183 x 61 cm which you can plug together easily. One side of the table is layed down on the coffee table whereas two feet are placed below the other side. I will now explain the single parts of the table.
The complete table.
The plates
For the plates I used 8 mm thick chipboard. A thicker plate is more stable but also heavier and more expensive. 8 mm works pretty fine for me but you shouldn't sit on top of the table.

The feet
At the end of each plate there is a foot mounted via a hinge. If the table is dismounted, you can easily fold up the foot and it does not take up much room.
The feet of the table.
Plugging together
At Obi, a local hardware store, there are special locks for closets which you can easily plug together. I attached them to both plates so you can plug the plates together.
The connection between the plates.
Positioning on the coffee table
Finallz, I glued some small pieces of wood below the table so the plate fits perfectlz onto the coffee table and cannot move anz more.
On the left the coffee table with the piece of wood below the plate.
On the table, you can now distribute the terrain or a grass mat. Alternatively, you can also put grass directly onto the plates, attach a piece of cloth or paint the plates which can be useful for large areas of water.