Saturday, October 30, 2010

How to write 10-bit ADC values to the 8-bit serial with Arduino?

The following code:
int sensorValue = analogRead(A0);
Serial.println(sensorValue, DEC);
Outputs (text):
0
1023
for the same input values the following code:
int sensorValue = analogRead(A0);
Serial.write(sensorValue);
Outputs (bytes)
00 63
The 0 got through, but the 1023 resulted in 63. This happens because:

  1. ADC resolution is 10-bit
  2. Thus the maximum value is 1023 (0x3FF)
  3. Serial.Write writes the more valuable 8 bits
  4. Serial.Write considers the ADC output as a 12 bit variable
  5. 63 (0x3F) are the 8 more valuable bits in this variable

So what you have to do is to fix this behavior
int upperByte = (sensorValue & 0x300) >> 8;
int lowerByte = sensorValue & 0xFF;
Serial.write(lowerByte);
Serial.write(upperByte);
Then you will get right aligned data:
00 00 FF 03
With
int upperByte = (sensorValue & 0x3FC) >> 2;
int lowerByte = (sensorValue & 0x3) << 6;
You get left aligned data for unsigned integers
00 00 C0 FF
With
int upperByte = (sensorValue & 0x3F8) >> 3;
int lowerByte = (sensorValue & 0x7) << 5;
You get left aligned data for signed integers
00 00 E0 7F

What are the open source CNC machines available?

Have you ever imagined printing circuit boards at your home? Yes, you can! Here is a small list of open source CNC machines:

Thursday, October 28, 2010

How to use a XBee module?

First you will have to configure it. The easiest way of doing so is following these steps:
  1. Download the configurator GUI: Digi X-CTU download
  2. Install and open X-CTU
  3. Select the Com Port.
  4. Open the "Modem Configuration" tab.
  5. Click on Read.
  6. You'll have to set one of your XBee modems as a "COORDINATOR AT" in the dropdown list "Function Set".
  7. Click on the "Write" button
  8. Leave the rest of the modems as "ROUTER/END DEVICE AT"
  9. Now your Coordinator will transmit to the rest of the modules

Wednesday, October 27, 2010

Open source plastic extruders


Plastic extruders are sold as 3D printers. While most commercial versions of such devices cost above US$ 20,000.00, fully-assembled open source ones are priced from $499.50. Here is a small list of those self-replicating machines:
Most of them use Arduino.

Tuesday, October 26, 2010

What happens if I only get the positive part of the signal?

I bought a Microphone and Amplifier eletronic brick, which came with the LM386 AmpOp.
The problem was that the Vin (pin 2) of this IC was grounded, thus the output renders only the positive part of the signal.
Instead of this:
I had then 2 solutions:

1. Either I could modify the hardware to input into the Vin a Vcc/2 voltage.
2. Or I could deal with the distortion via software.

To see if I could restore the signal I analysed its spectrum of the distorted signal:



And I had to restore it to look like this:

For such a simple signal a simple low pass filter would do. But I realized that it would be impossible for more complex signals.

Here is the Matlab/Octave code that I used to generate the plots above:

Fs = 44100;
time = linspace(0, 1, Fs + 1);

sine = sin(2 * pi * 440 * time);
plot(time, sine);
figure;
player = audioplayer(sine, 44100);
play(player);

positiveOnly = (sine + abs(sine)) / 2;
plot(time, positiveOnly);
figure;
player = audioplayer(positiveOnly, Fs);
play(player);

sineSpectre = fft(sine);
plot(time, sineSpectre);
figure;

positiveOnlySpectre = fft(positiveOnly);
plot(time, positiveOnlySpectre);

Can the UartsBee module be used as an USB wireless dongle?



Surely it can! From the product datasheet:

When BEE modules are plugged, it can either connect to PC via USB as wireless adapter, or connect to MCU via serial pins
as wireless add-on. You may find BEE modules including Xbee, GPS Bee, RF Bee, Blue Bee, Wifi Bee and many other

It also can be used as a USB to UART adapter! Be aware that only 1 of the 3 possible modes are available at a time:

1. USB to Bee
2. USB to UART
3. Bee to UART

Monday, October 25, 2010

Where does SeeedStudio Depot eletronic bricks come from?

I bought a Microphone Brick from SeedStudio.


It came with www.flamingoeda.com written on it. After some Chinese-to-English translation I found the project page and the store page from the original developers

error: 'PB4' was not declared in this scope



I saw this error when compiling the Music Shield code for ATMega328. The cause was that ATMega328 uses the following format for port names:

PORT + "letter" + "number" (ie. PORTB4)

instead of

P + "letter" + "number" (ie. PB4)

So all you have to do is to replace every occurrence of P[ABCD][1234] with PORT[ABCD][1234]

Friday, October 22, 2010

How to compile Arduino code?

1. First of all you'll need the Arduino IDE. You can download it freely from the official website:

http://arduino.cc/en/Main/Software



2. Then you will need to open the PDE file provided with the code you are trying to compile, using the Arduino IDE.
3. Click on the play button in the toolbar. It's the first button. This will try to compile the code.
4. If it doesn't works either you are compiling for the wrong board or you are missing some libraries
4a. You can try different boards by clicking in the Tools menu > Board
4b. You can install the missing libraries by searching for its name in the first compiling error, then googling for it, and then unzipping the library file in your arduino IDE\libraries folder

Wednesday, October 20, 2010

What is the maximum USART transmission rate for ATMega328?

While the ATMega can communicate using a 1Mbps serial connection, it only achieves 83KBps or 664Kbps effective data rate.

Sunday, October 17, 2010

How to combine the Music Shield with the Harness Kit?

The music shield collides with the battery in the Seeed Depot Harness Kits.
The only solution so far is to stack it on top of another eletronic brick.

What is the maximum sampling frequency for the Arduino ADC?

The ATMega 328 processor has multiple 10 bit ADC (Analog to Digital Converter) ports. By default it takes 111 microseconds to read an analog value, thus the default sampling frequency is 9KHz. But it is possible to configure the chip to read at 62.5 KHz with little loss of resolution, then the reading delay will be of 16 us.

You can use the following code to do that:

#ifndef cbi
        #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
    #endif
    #ifndef sbi
        #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
    #endif
    void setup() {
        sbi(ADCSRA,ADPS2) ;
        cbi(ADCSRA,ADPS1) ;
        cbi(ADCSRA,ADPS0) ;
    }

What is the maximum speed for the Arduino serial?

The terms USART, UART, serial and RS232 are sometimes used interchangeably.

The Serial Monitor that comes with the Arduino IDE only shows speeds up to 115.2 Kbps, but the processor itself can transmit in speeds up to 1 MHz. I haven't tested speeds greater than that.

Can I record audio using an analog port?

The ATMega 328 processor has multiple 10 bit ADC (Analog to Digital Converter) ports. The default sampling frequency is 9KHz. But it is possible to configure the chip to read at 62.5 KHz with little loss of resolution.

One problem is transmitting this data through the UART. An uncompressed 10 bit 8 KHz signal would consume 80 Kbps at minimum.

I have found that using the Microphone Brick with the Seeeduino Stalker, that I could read only the positive part of the audio signal. Maybe it's something I did wrong, I don't know.

Saturday, October 16, 2010

Can I program my Seeeduino Stalker using a XBee module?

In my experiments I couldn't program my Stalker via wireless. I had even to remove one of my XBee modules from my UARTsbee + Stalker setup.

The reason for that is that the XBee module scrambles the signal if it transmits data simultaneously with the UART.

What board should I choose in my Arduino IDE if I have a Seeeduino Stalker?

Arduino IDE misses all Seeeduino boards.
But since Seeeduino Stalker is Arduino compatible, for the 328 Stalker you may choose "Arduino Duemillanove or Nano w/ ATmega 328"

How to mount the Harness Kit for Mega/Arduino/Seeduino? (Tutorial)

There a few tricks you should know about the Harness Kit


  1. The chassis comes with a paper skin in both sides, you should remove it before mounting it
  2. Insert the plastic rivets from bottom to up without forcing their tips, they are white in the picture
  3. Put the main board over them
  4. Force their tips by pressing the pin, so the board will be fixed.
  5. Install the battery case
  6. The battery case comes with long wires. You should think about trimming them.
  7. The battery case will prevent some shields from being mounted uppon the main Mega/Arduino/Seeduino board. You should cut a piece of the battery case to solve this problem.
  8. Install the rubber pads

Why my Seeeduino Stalker has both a CR2032 battery holder and a JST battery connector?

The Seeeduino Stalker has a Master Switch, which turns off the device. But it also has a Real Time Clock (RTC) which needs power to run. To keep the RTC running when the Master Switch is set to off, you will need to insert 3V in the CR2032 battery holder pins, normally by inserting such battery in the slot.

The JST battery connector, in its turn, is one way of powering the whole board, others being using the Stalker as a shield, or powering it up through the UART connector.