Go Down

Topic: Firmata & REALbasic (Read 3045 times) previous topic - next topic



I'm new to the Arduino and would like to use it as a sensor platform to relay data to a host computer... which would send back instructions. I'm using REALbasic on my Mac and can control the LED on pin 13 as long as my sketch knows what to listen for. Rather than create a communications protocol, I read about Firmata and thought that would be perfect. However, for the life of me, I can't figure out the messaging syntax. I was wondering if someone could post the two commands I should send over the serial port to set the pin mode and activate the LED... and that should be enough for me to decipher the rest.

For example, I'm sending "F4 0D 01" to set the pin 13 to output. Is that right? I think the second command should begin with "9D" (turn on pin 13) but I don't have a clue what the LSB and MSB bytes should be.

Any help would be greatly appreciated. For all I know, I'm off in left field.


Hi JohnRD,

You are correct. To set Pin 13 to output you send the 3 bytes - Hex values F4 0D 01. To actually set the values or states (on/off) of the digital pins you address an entire port at once. On a Duemilanove board the ports (as Firmata sees them) are set up as follows:

Port 0 - pins 2,3,4,5,6,7
Port 1 - pins 8,9,10,11,12,13

You turn these digital pins on/off by sending a 3 byte message in the following format:
Port | 1st Byte | 2nd Byte

Where Port is 0x90 for Port 0, 0x91 for Port 1 ... (and that's all you need for the Duemilanove it only has that many digital pins)

The 1st and 2nd Bytes are the 'bitmasks' for the port in question. Think of the bits in the 1st and 2nd Byte as individual switches for the pins in that port.

So to send a message to turn on Pin 13 you send:
MessagePort1st Byte2nd Byte
Hex version of message0x910x200x00
Hex and byte bits version0x910010000000000000

The second row in the table shows the Hex values. In the third row I've shown the bits in each byte. Pin 13 is actually the 6th pin in Port 1 so to turn it on you set the 6th bit to a '1'.

Each time you send a digital I/O message you are actually setting the state of ALL the pins in that port. So in the example above, when you turned ON pin 13 the message did turn on pin 13 but it also turned OFF all the other pins (all those other bits set to '0').

To effectively use Digital I/O Messages you have to keep a track of which pins are on or off in your software. If pin 10 is on and you decide to then turn on pin 13 and use the message above you'd be turning pin 13 on but 'accidentally' turning off pin 10.

Below are the bits that correspond to each pin on the board:

Port 0 (pins 2,3,4,5,6,7)
1st Byte                          2nd Byte
X  X 13 12 11 10  9  8      X  X  X  X  X  X  X  X

Port 1 (pins 8,9,10,11,12,13)
1st Byte                          2nd Byte
X  X  6  5   4   3   2  X      X  X  X  X  X  X  X  7

In Port 1 pins 0 and 1 are RX and TX so aren't used. Wherever you see an X above you should always simple send a 0 (zero).

So... let's say you want to turn on pins 13, 11 and 8 you'd send:
0x91 followed by the following 2 bytes of 'bitmasks' - 0 0 1 0 1 0 0 1  0 0 0 0 0 0 0 0
In Hex that'd be 0x91 0x29 0x00.
If you then want to turn off only pin 11 you'd have to send 0x91 followed by 0 0 1 0 0 0 0 1  0 0 0 0 0 0 0 0
Which sets the bit for pin 11 to 0 but leaves the bits for pin 13 and 8 set to 1

I hope this was of some help and that it made sense.

Happy bitmasking,



Thanks, Andrew. I never would have figured that out on my own.


I've uploaded firmata standart to my arduino and now i'm trying to turn Led13 On with arduino serial monitor...

but so far i haven't figured it out about how i should do it...

i'm doing this:

at 115200baud

I write "F4 0D 01" and I press send
then I write"90 91 20 00" and I press send...

but led 13 doesn't turn On :S

(sorry for bad english but i'm portuguese :) )

What am I doing wrong?

P.S- Do i have to send chr(13) = CR ?


BTW Andrew, I've been using your app, FirmataVB...

An excellent application  ;)


It's not working from the serial monitor because the serial monitor sends characters, not hex.   For example, sending "F4" from the serial monitor sends the ASCII characters "F" and "4", which are hex 0x46 and 0x34 respectively.  Neither of these are the hex 0xF4 you're trying to send.  You'll either have to write a program that sends your hex values as you want them to be sent, or find another serial port terminal program that will allow you to send hex values.


Thanks now it's working :D
I'm now able to set digital ports to inputs or outputs and set them HIGH or LOW...

But now I don't know how do I control analog outputs and how do I read digital and analog inputs..


Quite a newbie, and struggling to get the Arduino to talk with Realbasic with Firmata. Does anyone have an example program for this? I am using the newest version of Realbasic, (actually it is now Realstudio) on a Mac.

I have been trying to use the firmata2.rbp. The connecting works fine. But then I enter the command PINMODE, pin 13 and OUTPUT (hit send), and then DigitalWrite, pin 13 and HIGH. But nothing happens after these commands, with the LED on pin 13.

Really hope anyone can help with a sample, or with some other useful hints.

Go Up