AdSense

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.

Hardware

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.

Software

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;
  Mirf.init();
  
  /*   * 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.   */
   
  Mirf.config();

}

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.     */
     
    Mirf.getData(data);
    
    /*     * Set the send address.     */
     
     
    Mirf.setTADDR((byte *)"clie1");
    
    /*     * Send the data back to the client.     */
     
    Mirf.send(data);
    
    /*     * 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');
fopen(s);


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:

fclose(s);

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

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

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);
  digitalWrite(ledPin,HIGH);
  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)
  {
    delayMicroseconds(100);
  }
  
  //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.

Tuesday 13 January 2015

Arduino - read keypad

(Deutsche Version) Today I want to explain how to read out a simple keypad with an Arduino, as seen in the following image. You could use this keypad from ebay: Keypad.
The keypad has 8 pins. 4 in each case are for the rows respectively the columns. A pressed key connects two of these pins. 4 pins are used as an output on the Arduino, the other 4 pins as an input. I used pin 22, 24, 26 and 28 as output and 30, 32, 34, 36 as input so you can easily attach the keypad to the Arduino using a pin header.
Now a voltage is applied to one output after the other and the input pins are measured whether they receive this voltage. This shows directly which key was pressed. My code reads the pressed keys and sends them via the serial bus to the computer as soon as a key state has changed.

//For different sizes of Keypads, you can adjust the numbers here. Important: Also change the keyValues array!
const int numOuts = 4;
const int numIns = 4;
//These are the 4 output pins, adjust it if you use a different pin mapping
int outs[numOuts] = {22, 24, 26, 28};
//These are the 4 input pins, adjust it if you use a different pin mapping
int ins[numIns] = {30, 32, 34, 36};
//This array contains the values printed to the different keys
char keyValues[numOuts][numIns] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
//This array contains whether a pin is pressed or not
boolean pressed[numOuts][numIns];

void setup() {  
  Serial.begin(9600);
  //Define all outputs and set them to high
  for (int i = 0; i < numOuts; i++)
  {
    pinMode(outs[i], OUTPUT);
    digitalWrite(outs[i], HIGH);
  }
  //Define all inputs and activate the internal pullup resistor
  for (int i = 0; i < numIns; i++)
  {
    pinMode(ins[i], INPUT);
    digitalWrite(ins[i], HIGH);
  }
}

//Read whether a key is pressed
void KeyPressed()
{
  for (int i = 0; i < numOuts; i++)
  {
    //Activate (set to LOW) one output after another
    digitalWrite(outs[i], LOW);
    //Wait a short time
    delay(10);
    for (int j = 0; j < numIns; j++)
    {
      //Now read every input and invert it (HIGH = key not pressed (internal pullup), LOW = key pressed (because the output was set to LOW)
      boolean val = !digitalRead(ins[j]);
      //If the value changed, send via serial to the computer and save the value in the "pressed" array
      if (pressed[i][j] != val)
      {
        char str[255];
        sprintf(str, "%c pressed: %d", keyValues[i][j], val);
        Serial.println(str);
        pressed[i][j] = val;
      }
      
    }
    //Deactivate (set back to HIGH) the output
    digitalWrite(outs[i], HIGH);
  }
}

//Yeah, do this forever...
void loop()
{
  KeyPressed();
}