Main Site Documentation

Sending ttl data at specific baudrate using gpio pin



I would like to ask the experts on this forum for a push in the right direction.
I need to send binary data over a pin. I tried using one of the serial ports, but then the data is changed to serial data (added a start and stopbit etc.)

What I need is to send my hex string e.g. “A135782” as ttl signals at 1200 baud.
My biggest problem here is the timimg of the pin. When I use a serialport, I can specify the baudrate. When using a standard pin, I must toggle its state myself.
I tried using a timer, but since I need a timer smaller then 1 milisecond, I don’t know how to code this.

Any help or suggestions are much appreciated.

Best regards,



You can use output compare but the question is, what are you trying to do?!


Hi Gus,

First, let me thank you for your reaction.

Let me explain a bit more.
I’m trying to send hex data to a uhf transmitter. The transmitter has a “data” input, on which it accepts ttl level signals. The transmitter is used to broadcast the data and this is received by some pagers. These pagers actually receive the data and, if they decide it was addressed to them, they should display the message.

Could you explain a bit more about the output compare you mentioned?


Output compare takes in an array of delay values that re then converted to stream of digital signal. You can use it to simulate serial, TV remote signal or what you are describing here.


Hi Gus,

That sounds interesting. I did not find anything about this in my getting started e-book, but google quickly learned me that I was using an old version of the book. In a newer release, I found some info about it, but I don’t quite understand ho to use this in my situation. I should probably mention that working with bits and bytes is not my area of expertise… I have a basic knowledge of electronics, but I’m mostly programming database applictions. So please be patient :-[

Could you perhaps point me in the right direction with a small sample? Lets say I wanted to send out a hex string of “1234567” to a pin. How shoud I proceed?

Thanks again for your time.


It will take pages to explain this. Please see the GHI library documentation.

…start with something simple, like toggle a pin to blink LED to understand the functionality.


Here is also an example software UART that uses output compare feature


And why don’t you use a serialport?



Because his protocol isn’t strictly RS232-type serial, you can’t inhibit the start bit (but you can inhibit the stop bits) with the .NET framework.


I’m working with the Chipworx board for quite some time now. I do know how to toggle a pin or read / write serial data from a comport. I just don’t quite understand how this exactly works on serial level. Also I don’t exactly understand how the hex code is ‘translated’ into bytes and how these bytes are translated to ttl signals. Since I normally use a comport, I did not have the need to understand.

I will have a look at the link you provided.

@ Wouter: When I use a serialport and monitor the output on a scope, I notice that certain extra info is added to the signal. For example, when I send 0xAA to the serialport, I need to see just 10101010 on my scope. Since this is not the case, I assume (perhaps I’m wrong) that this has to do with the startbit, stopbit etc. If you could point out what I’m doing wrong I would be more than happy to use a comport.
Thanks for your reaction btw. ;D

Edit: I just looked at the provided link and this was the code I was talking about before. I do not completely understand what is hapening in the sendbyte method, so I find it dificult to adapt it to my needs. I will give it a try later and let you know what happened.


The right place to start then is in searching the web and understanding how data is serialized and sent on wires.


Hi Gus,
I just read the wiki about uarts. Altough interesting, I don’t quite see how this helps me. A uart will actually provide the frame around my data by adding a startbit and a stopbit. That was the reason I wanted to use a standard pin in the first place, because I did not want to use the uart.

You pointed me to the output compare functionality. Could you please explain how to send a byte at 1200 baud using this technique? I think I understand what needs to happen signalwise, now its time to learn how to achieve this.

What I tried was something like this:

Declare an array with 8 bits of data in it.

Use a for loop to itterate trough the array members
Make the outputpin equal to the value of the current array member
Make the thread sleep for 0.833 ms (100/ 1200 = 0.833)

I’m working on my iPad now, thats why I’m not providing actual code, but you get what I mean right?

This actually kind of works, but ofcourse the timing sucks. First of all, I could not get the sleep method to accept something smaller than 1ms. Second, even with one ms, I still see way to slow results on the scope. Don’t understand why, but its what I saw.
I also tried a similar setup with a timer, but sort of same result.

How could I leverage the output compare into sending my bytes at 1200 baud?

Thanks again for your help.