Tag Archives: howto

Programming the Arduino Pro Mini with USB Mini

Everyone, who has been making a project based on Arduino probably wanted the board to be smaller than the basic unit.

A variety of smaller, but compatible with Arduino, boards have been developed. One of them is SparkFun’s Arduino Pro Mini. Currently, it has ATmega328 onboard and its dimensions are 33 mm x 18 mm. It’s available in two versions. The first runs on lowered (3,3V) supply voltage, sacrificing clock frequency (up to 8 MHz). The latter version is a copy of an ordinary Arduino – works with 5V supply and the maximum frequency is 16 MHz.

If the board is so small, then what’s lacking? So, there is no USB/Serial converter (however, it lowers the price also). Because of that we need an additional device for programming it – USB/Serial TTL converter.

One of USB/Serial converters in Nettigo’s offer is Arduino Mini USB. Wiring it up and programming could cause some problems, so, here is a small HOWTO:

Manual programming

After resetting the device, Arduino bootloader is active for about a second. It waits for data on Serial and, if some binary data is present – transmission will be stored in Flash memory and started. Therefore, the simplest way is to connect Arduino Mini USB and Pro Mini like the following:

  • Connect USB Mini and Pro Mini grounds (GND z GND)
  • Supply voltage from USB Mini (+5V) to Vcc on Pro Mini
  • RX from USB Mini to RX1 on Pro Mini
  • TX from USB Mini to TX0 on Pro Mini

The outputs on USB Mini are unlabelled, so here is a pinout schematic (USB socket is at the left of this picture):

USB Mini pinout

USB Mini pinout

In practice, it looks like this:

Manual reset circuit

Manual reset circuit

A sketch needs to be compiled at first (the icon with a triangle) and compiled after that. In order to do it, press the reset button on Arduino Pro Mini and the upload button in Arduino IDE simultaneously (before that, choose the proper board in Tools/Board menu). TX i RX on USB Mini will start blinking and the sketch should be on Pro Mini after a while.

Automatic Reset or Software Reset

Since a long time ago, a regular Arduino can be easily programmed by clicking Upload in the IDE with no need for resetting it manually. In the case of Mini boards, often placed in difficult spots, this method is much better.

To accomplish it, we only have to connect USB Mini’s RTS output to any of RST inputs on Pro Mini:

Automatic (program) Arduino Pro Mini reset

Automatic (program) Arduino Pro Mini reset

I’ve used Bus Pirate probes here, because I hadn’t any pin headers soldered. From now on the programming only requires clicking Upload in Arduino IDE.

Remember the colours – a game

The following article is based on this entry on Newton’s blog (in Polish). All the pictures, program code and the video clip came from there.

This project’s goal was to use as many parts from the Arduino Starter Kit, as possible. So, what is this project exactly? As the title itself suggests, it’s a game, whose object is to remember a colour sequence and recreate it. At the very beginning the sequence consists of 3 colours only, hovever, the difficulty level increases – an extra colour is added each time. Every colour has a different tone assigned. This tone is emitted from a buzzer when the corresponding colour is displayed, so the game involves two senses, both sight and hearing. User interface consists of: RGB LED diode displaying the colours, 8 red LED diodes arranged in a row to display which colour in turn we are setting and two microswitch type buttons. By pushing both in the same time we are starting the game, the left button chooses the colour, the right – accepts. There are two more LED diodes, indicating whether we have recreated the sequence properly (left LED), or not (right LED).

Whole device mounted on a breadboard looks like the following:

Device mounted on a breadboard

You can see (and hear) how it works on this video clip. Hereunder is the circuit schematic made in Fritzing:

Circuit schematic

Circuit schematic

All the resistors used are 220Ω and the IC in the centre of the breadboard is 74HC595 shift register, which allows to drive eight LEDs using only three Arduino digital pins. This was described in “Shifted LEDs” article.

When everything is connected, we should take care of the code. At the beginning we must declare where we have connected what (to which input/output). The following code is responsible of it:

int latchPin = 8, clockPin = 12, dataPin = 3; //shift register
byte data = 0; //register data

int led8 = 7, led9 = 6; //additional LEDs
int ledR = 11, ledG = 10, ledB = 9; //LED RGB
int buttonLeft = 5, buttonRight = 4; //buttons
int speakerPin = 13; //buzzer

Here comes the initialization part – setting pin as input/output, turning internal pull-up resistors on and so on:

void setup()
{
  Serial.begin(57600);
  for(int i=3;i<=13;i++) // setting all the pins
    pinMode(i, OUTPUT); // from 3 to 13 as outputs
  pinMode(buttonLeft, INPUT); // setting the button pins
  pinMode(buttonRight, INPUT); // as inputs

  digitalWrite(buttonLeft, HIGH); // turning internal pull-up
  digitalWrite(buttonRight, HIGH); // resistors on
  randomSeed(analogRead(0)); // initializing the pseudorandom number generator
  // with the value read from analog input no. 0 
  // - each time it's different
  clearLeds();
}

If you are wondering what stands for such a strange notation:

 a ? b : c

here is the explanation.

The main loop of our program looks like the following:

void loop()
{
  effect();
  clearLeds();
  if(waitForButton() == 3)
    return;

  for(int j=3;j<=8;j++)
    if(!playLevel(7, j))
      return;
}

Consecutively: at the beginning, effect() function turns on visual and light effects, clearLeds turns all the LEDs off. After that, unless the user pushes both buttons at the same time, what will cause a reset, playLevel function starts successive levels of the game, increasing the second parameter (number of the lights to remember) by 1. The first parameter denotes the total number of colours (from 0 to 7). Of course, we can lower it, but then the game will become too easy. It’s better to add another colours using setColor function (changing parameters in setRGB) and upgrade it. Then, we can put the total number of colours (remember – the numeration starts from 0!) as the first parameter of playLevel function to make the game more difficult and hence – more addicting ;).

The whole program code with comments is available here. I think it’s clear enough and doesn’t require any additional explanations.

How to connect switches

Switches (buttons, pushbuttons, tact switches) are commonly used with Arduino. But, for beginners, it’s sometimes irritating when it comes to wire them up correctly. Some of their terminals are shorted with each other. Such a switch doesn’t work if putted in reversely. Because of that, here’s some words of explanation.

For instance, we have a button like this:

Just an ordinary button.

As you can see, it has four terminals organised in two pairs. These pairs are shorted when we are pushing the button. Which of them are which? Maybe this picture will be helpful:

Internal connections of the button

And here is a photo with a switch symbol shown:

Button with schematic

Now it’s clear that proper orientation is the crucial thing. Turning the switch by 90 degrees causes permanent short – the button will act as a jumper, not as switch.

For even cleaner explanation, example of a working circuit:

Circuit with a button

And this is how it looks with schematic:

Circuit with its schematic

This switch shorts the resistor to ground. How to detect whether the button is pressed or not? Read more in the article about Family Feud.

Self-made Arduino shield – making a PCB

Sooner or later, everyone bitten by Arduino bug would want to make his own extension. At the beginning Proto Shield may be sufficient.

When we have a working prototype, we might want to convert it into a series model, so PCB board will be needed.

PCB

Time for self-made PCB CC by http://www.flickr.com/photos/odinhg/

On Arduino Forum there is a link to a very nice tutorial, how to make our own PCB board, especially for Arduino shields.

Aaron Eiche, author of the article is a member of DorkbotPDX group, which makes orders for PCBs and everyone can order with them the price is quite attractive – $5 for square inch of a double sided board + $15 for shipment (worldwide). It must be ordered in multiples of 3.

Arduino Bluetooth on Ubuntu

Arduino Bluetooth – Arduino without USB port, with Bluetooth module instead. Ideal solution if wireless communication with Arduino is needed.

At first – some differences to an ordinary Arduino Duemilanove. Arduino BT is based on ATmega168, so we have a little less memory at disposal. There is no USB port, so we have to power the board on our own.
If we’re talking about power – Arduino BT is vulnerable to supply voltage polarity, so connect ground and Vcc to the board with attention! What’s more, maximum supply voltage is only 5,5V! Fortunately, thanks to DC/DC converter, the minimal voltage is only 1.2V.

Arduino Bluetooth

Arduino Bluetooth

Because there is no USB port, the programming is also performed through Bluetooth. A short instruction how to do it – Ubuntu 9.10.

I didn’t utilized Ubuntu GUI to configure the connection, probably it also will work ;)

At first (of course after turning Arduino BT on) we search for available devices:

$ hcitool scan
Scanning ...
	00:07:80:91:30:09	ARDUINOBT

Here is the found module. 00:07:80:91:30:09 – it’s MAC address of the device and of course it’s different for each device. It should be replaced with the proper number of your Arduino everywhere below.

Now we are connecting to the module:

$ sudo hcitool cc 00:07:80:91:30:09
$ sudo hcitool auth 00:07:80:91:30:09

Possibly it will ask you for PIN – default is 12345.

We need informations about this device:

$ sdptool browse 00:07:80:91:30:09
Browsing 00:07:80:91:30:09 ...
Service Name: Bluetooth Serial Port
Service RecHandle: 0x10000
Service Class ID List:
  UUID 128: 00001101-0000-1000-8000-00805f9b34fb
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 1
Language Base Attr List:
  code_ISO639: 0x656e
  encoding:    0x6a
  base_offset: 0x100

Important informations are RFCOMM and Channel 1. RFCOMM is a serial port emulation protocol for radio transmission.

Now we can write to /etc/bluetooth/rfcomm.conf:

rfcomm0 {
	bind yes;
	device 00:07:80:91:30:09;
	channel	1;
	comment "Arduino BT";
}

Of course channel may have the same value as returned by sdptool. Having it written in the configuration file we are able to connect:

$ sudo rfcomm connect 0
Connected /dev/rfcomm0 to 00:07:80:91:30:09 on channel 1
Press CTRL-C for hangup

Now it remains to set minicom -s the port to /dev/rfcomm0 and we should be connected to Arduino serial.

In theory :) Arduino BT should be programmed with the following code (original without formatting) by deafult:

/* BT test 01
* ——————
* Massimo Banzi
*
*/

int LED = 13; // select the pin for the LED
int RESET = 7;

void setup() {
pinMode(LED,OUTPUT); // declare the LED’s pin as output
pinMode(RESET,OUTPUT); // declare the LED’s pin as output
Serial.begin(115200); // connect to the serial port
digitalWrite(RESET, HIGH);
delay(10);
digitalWrite(RESET, LOW);
delay(2000);
Serial.println(“SET BT PAGEMODE 3 2000 1”);
Serial.println(“SET BT NAME ARDUINOBT”);
Serial.println(“SET BT ROLE 0 f 7d00”);
Serial.println(“SET CONTROL ECHO 0”);
Serial.println(“SET BT AUTH * 12345”);
Serial.println(“SET CONTROL ESCAPE – 00 1”);
Serial.println(“SET CONTROL BAUD 115200,8n1”); //first release 19200
}

void loop () {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
Serial.println(“ciao”);
delay(1000);
}

It should – but from my humble experience, it usually comes from a factory with classic blink – digital pin 13 lights up and blows out. So it is in the code above, but it also should write “ciao” message on the serial. I haven’t yet succeeded to get the message with ‘fresh’ Arduino BT (at /dev/rfcomm0 there was silence all the time), however the 13th pin in fact was blinking (Arduino BT hasn’t LED diode on board, so you have to connect it).

It only remains to load the code above on the board. The best idea is to set BT in Arduino IDE permanently and add the following code to ~/.arduino/preferences.txt file:

serial.stopbits=1
serial.databits=8
serial.parity=N
serial.port=/dev/rfcomm0
serial.debug_rate=115200
serial.burn_rate=115200
board=bt

Now the program can be loaded without any problem. Note – even if Arduino tells it uses /dev/ttyS0 or USB0, with the abovementioned entry to preferences.txt it should be loaded through rfcomm0.

Loaded without any problem – but only when you know how. Well, the latest Arduinos equipped with USB have a firmware allowing to load new code at any time (the board is resetted through USB and new code is loaded). Arduino BT firmware doesn’t support this useful function, so we have to reset the module manually (S1 button) and press “Upload” in IDE in one second.

After such a procedure the program will be loaded easily and from now on minicom will show ciao.

Arduino BT is available on nettigo.pl – in small amounts, bigger are on request (maximum waiting time – a month).

3 in one – RGB diodes

Our blog has received support – a new writer (attentive reader probably know already who it is :) ), so new interesting posts are being prepared, while I am preparing, as usual, some colorful lights :) .

Let’s begin from the end – a short movie. It isn’t impressing, because mobile phone camera doesn’t reproduct the colours well.

What to do… Well, maybe pictures of the assembled circuit.

RGB LED

RGB LED

Now it’s already known what the title RGB diode is – three LED diodes in one: Red, Green, Blue.
Common cathode, so easily controlled from Arduino – only connection (through 220 Ohm resistor) between proper terminal and Arduino digital pin is required to control one colour.

Which terminal is which? The following picture will be helpful:

Describing from the left side, we have: blue, green, ground, red. The longest terminal of it is ground and this lets us see how to connect the diode.

What gives us such a diode? Well, we can produce every colour by mixing three base colours in varying amount. To illustrate this, I will present the code used in first example. The idea is to light every single colour with different intensity (using PWM) and different frequency.
We are getting a mixing colours effect in this way:

int LED1 = 9;
int s1 = 255;
int LED2 = 10;
int s2 = 255;
int LED3 = 11;
int s3 = 255;

unsigned long int tme;

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
};

void loop() {

  analogWrite(LED1, s1);
  s1 = sin(tme*tme/50000.0)*128+128;

analogWrite(LED3, s3);
  s3 = sin(tme*tme/100000.0)*128+128;

  analogWrite(LED2, s2);
  s2 = sin(tme*tme/70000.0)*128+128;

  delay(15);

  tme++;
};

tme is variable representing the time elapsed. In loop, tme square value is divided by a sort of values and sinus is calculated from them. Thanks to it we have three different waves (of different frequencies) and every controls one colour. Since square is a non-linear increasing function, the phase offset of every wave should vary across time (I think so, but maybe this should be checked out precisely). Because of that the colours are mixed differently and not in a fixed sequence.

The side effect is that with time, the changes become more frequent and the diode is blinking faster and faster…

If someone needs such a diode – it’s available in Nettigo.

Balancing robots – for dummies?

Balancing robots are those, which can maintain balance by themselves. And how it looks? For example like this.

Author of the robot above began a interesting thread on Arduino forum: Balancing robot for dummies, where he promises to describe everything in a simple and aproachable way. If you are interested in this kind of robots – trace this thread with attention.

Tilt sensor

Nettigo offers tilt sensors with 30° and 60° (exactly from -30° to +30°) operating range.
Tilt sensor is a kind of switch. The difference between it and regular switches is that the current starts to flow through it when it’s tilted. Such a sensors are commonly user in car alarm system They are monitoring car’s position and triggering the alarm when the car is dragged by a wrecker truck, for example.
It looks like this:

Hardware

Utilizing the sensor isn’t difficult. It’s similiar to every ordinary switch. Only a free Arduino digital pin is needed. When the pin is in input mode, its state remains unknown (random) until a certain voltage is applied to it. Applying a “GND” sets it into “LOW” state, and providing 5V causes “HIGH” state.

If we have the sensor wired to a digital pin and “GND”, we know the state will switch to “LOW” when the current will begin to flow. When the current doesn’t flow, the state is going to be reversed – “HIGH”. Presently, the state is undefined. Thus, to define the state we will use built-in “Pull Up” resistor. It causes setting a pin into “HIGH” state when no voltage is applied to it.

Software

We’re taking advantage of the sensor reading the state of a selected digital pin. First, we must set the pin as “INPUT” in “setup” section.

pinMode(pin_number, INPUT);

And activate internal “Pull Up” resistor.

digitalWrite(pin_number, HIGH);

After that, in “loop” function you can read pin’s state and thus the sensor’s position with “digitalRead” function.

variable = digitalRead(pin_number);

Example program, lighting the “L” diode up during a tilt.

// Defining a pin for the tilt sensor
#define SENSOR_PIN 2
// Defining a pin for the LED diode
#define LED_PIN 13

void setup()
{
	// Setting sensor's pin as input
  pinMode(SENSOR_PIN, INPUT);
  // Turning internal Pull Up resistor on
  digitalWrite(SENSOR_PIN, HIGH);
  pinMode(LED_PIN, OUTPUT);
}

void loop()
{
	// Checking sensor's state
  if (digitalRead(SENSOR_PIN) == HIGH)
  {
  	// Action if off
    digitalWrite(LED_PIN, HIGH); // Turning the LED on
  }
  else
  {
  	// Action if on
    digitalWrite(LED_PIN, LOW); // Turning the LED off
  }
}

Assembled module

A task

As every man well knows, a woman holds in her purse only treasures. These are so valuable, that no-one is allowed to browse it. In order to protect poor ladies from nosy people I resolved to build a purse alarm.

Elements

The following parts will be needed:

  • Tilt sensor for sensing the position of the purse
  • Piezo buzzer for alarming

I assembled the elements as on the following schematic. I used pin 2 as sensor’s input, pin 3 as output for piezo buzzer and pin 13 for LED diode.

The code isn’t much different from the previous, except from additional sound procedures.

First of them is “tone”, turning the sound on.

tone(speaker_pin_number, frequency);

The second – “noTone”, turning it off.

noTone(speaker_pin_number);
// Defining a pin for the tilt sensor
#define SENSOR_PIN 2
// Defining a pin for the LED diode
#define LED_PIN 13
// Defining a pin for the piezo buzzer
#define PIEZO_PIN 3

void setup()
{
  // Setting sensor's pin as input
  pinMode(SENSOR_PIN, INPUT);
  // Turning internal Pull Up resistor on
  digitalWrite(SENSOR_PIN, HIGH);
  pinMode(LED_PIN, OUTPUT);
}

void loop()
{
  // Checking sensor's state
  if (digitalRead(SENSOR_PIN) == HIGH)
  {
    // Action if on
    digitalWrite(LED_PIN, HIGH); // Turning the LED on
    tone(PIEZO_PIN, 600); // Alarm sound
  }
  else
  {
    // Action if off
    digitalWrite(LED_PIN, LOW); // Turning the LED off
    noTone(PIEZO_PIN); // Turning alarm sound off
  }
}

It’s easily noticeable that the alarm is too sensitive and every shock (even foot stamping) causes “screeching” of the buzzer. This would not appeal to the purse’s owner.

What is the difference between shocks and poking around in a purse?

The easiest way to distinguish them is switching speed of sensor. Shakes are switching it in thousandths of a second, while moves caused by a lifter – from tenths of a second to a few seconds. So, the time in which the sensor turns on must be counted and, if it’s longer than 0.1 s (moving the purse), the alarm should be triggered.

For this case, I used “millis” function, which returns how many miliseconds have elapsed from turning Arduino on. It could be applicated to storing the time from turning the sensor on. When a previously set value is reached, the alarm is turned on.

// Defining a pin for the tilt sensor
#define SENSOR_PIN 2
// Defining a pin for the LED diode
#define LED_PIN 13
// Defining a pin for the piezo
#define PIEZO_PIN 3
// Defining sensitivity time
#define SENSOR_TIME 100

// Variable storing the switching time
unsigned long sensor_time;

// Function invoked, when the alarm is on
void alarm_on()
{
	digitalWrite(LED_PIN, HIGH); // LED on
  tone(PIEZO_PIN, 600); // Alarm sound
}

//  Function invoked, when the alarm is off
void alarm_off()
{
  digitalWrite(LED_PIN, LOW); // LED off
  noTone(PIEZO_PIN); // Turning alarm sound off
}

// Function checking sensor's state
void check_sensor()
{
  // Checking sensor's state
  if (digitalRead(SENSOR_PIN) == HIGH)
  {
    // Action if off

    if (sensor_time == 0) // Checking if the state was changed
    {
      sensor_time = millis(); // Storing the sensor's activation time
    }
    // Checking if the state of sensor has been lasting for a specified time
    else if (millis() - sensor_time > SENSOR_TIME)
    {
      alarm_on();
    }
  }
  else
  {
    // Action if on
    sensor_time = 0;
    alarm_off();
  }
}

void setup()
{
  // Setting the sensor's pin as input
  pinMode(SENSOR_PIN, INPUT);
  // Turning internal Pull Up resistor on
  digitalWrite(SENSOR_PIN, HIGH);
  pinMode(LED_PIN, OUTPUT);
}

void loop()
{
  check_sensor();
}

Real alarm

Our purse alarm has one more disadvantage. It turns off immediately when the purse returns to its position. This is not a way real alarm works!
Professional alarm systems also have varied sounds. Here we will use “millis” function again. In the first place we are going to count, how much time was elapsed since triggering the alarm. It will be set off only when a certain amount of time will elapse, in order to deter and attract attention more effectively.
In the second place I employed time measuring to check the duration of signal tone, so as to switch it cyclically between high and low pitches. This way our purse alarm will be better distinguished from other noises.

// Defining a pin for the tilt sensor
#define SENSOR_PIN 2
// Defining a pin for the LED diode
#define LED_PIN 13
// Defining a pin for the piezo
#define PIEZO_PIN 3
// Defining sensitivity time [ms]
#define SENSOR_TIME 100
// Defining alarm duration time [ms] (5000 ms = 5 s)
#define ALARM_TIME 5000

// variable storing sensor's switching time
unsigned long sensor_time;
// variable storing alarm duration
unsigned long alarm_time;
// variable storing pitch change time
unsigned long signal_time = 0;
// variable storing the pitch value
// 0 - no sound
// 1 - first tone
// > 1 - second tone
byte sound_tone = 0;

// alarm siren function
void alarm_signal()
{
  if (sound_tone != 0) // checking if the siren should be turned on
  {
    if (millis() - signal_time > 750) // checking tone duration
    {
      signal_time = millis(); // storing the time when the tone was turned on
      sound_tone = ~sound_tone; // turning on the second tone
    }

    if (sound_tone == 1) // first tone
    {
      tone(PIEZO_PIN, 600);
    }
    else // second tone
    {
      tone(PIEZO_PIN, 1200);
    }
  }
  else
  {
    noTone(PIEZO_PIN); // Sound off
  }
}

// Function invoked when the alarm is off
void alarm_on()
{
  alarm_time = millis(); // storing the time when the tone was turned on
  digitalWrite(LED_PIN, HIGH); // LED on
  sound_tone = 1; // Turning alarm siren on
}

// Function invoked when the alarm is off
void alarm_off()
{
  if (millis() - alarm_time > ALARM_TIME) // checking alarm duration
  {
    digitalWrite(LED_PIN, LOW); // LED off
    sound_tone = 0; // Turning alarm siren off
  }
}

// function checking sensor's state
void check_sensor()
{
  // checking sensor's state
  if (digitalRead(SENSOR_PIN) == HIGH)
  {
    // Action if off

    if (sensor_time == 0) // checking if the state was changed
    {
      sensor_time = millis(); // storing sensor's activation time
    }
    // Condition, if sensor's state lasts for a specified time
    else if (millis() - sensor_time > SENSOR_TIME)
    {
      alarm_on();
    }
  }
  else
  {
    // Action if off
    sensor_time = 0;
    alarm_off();
  }
}

void setup()
{
  // Setting the sensor's pin as input
  pinMode(SENSOR_PIN, INPUT);
  // Turning internal Pull Up resistor on
  digitalWrite(SENSOR_PIN, HIGH);
  pinMode(LED_PIN, OUTPUT);
}

void loop()
{
  check_sensor();
  alarm_signal();
}

This is only one of many applications of tilt sensor. If you have any ideas or questions – leave a comment or use the mail.

Source codes: