scenprogs-logo

Wireless uart communication with 2 atmel microcontrollers (ATtiny2313a) and 433MHz modules

projects › atmega 26.11.16 scenprogs

Since these 433 Mhz modules are very low priced they draw a high interest for using in hobbyist projects. This article covers  how I established a wireless serial communication through the build in uart interface of atmega microcontrollers.

These 433MHz modules (transmitter and receiver) are simple pass-through integrated circuits which use the  Amplitude-Shift Keying (ASK) modulation. They just send the data from transmitter to the receiver working like if you have a wired serial connection between each mircocontroller. But going wireless also means to confront signals and data from other devices which are obtainable for the involved receiver. While those modules process in the often used 433 mhz band the obtained data has to be verified – the use of a protocol with data verification is required.

receiver module parameters

1. Product Model: MX-05V
2. Operating voltage: DC5V
3. Quiescent Current: 4MA
4. Receiving frequency: 433.92MHZ
5. Receiver sensitivity: -105DB
6. Size: 30 * 14 * 7mm
7. External antenna: 32CM single core wire, wound into a spiral

transmitter module parameters

1. Product Model: MX-FS-03V
2. Launch distance: 20-200 meters (different voltage, different results)
3. Operating voltage: 3.5-12V
4. Dimensions: 19 * 19mm
5. Operating mode: AM
6. Transfer rate: 4KB / S
7. Transmitting power: 10mW
8. Transmitting frequency: 433Mhz
9. An external antenna: 25cm ordinary multi-core or single-core line

test circuits

For testing I constructed 2 seperate circuits with 2 ATtiny2313a micrcontrollers with 1 transmitter and 1 receiver. The configuration is very easy and clear because the modules only need power apply (Vcc, GND) and 1 single data for transmitting (IN) and receiving (OUT).

transmitter circuit

img_20161103_095709

The attiny2313a is running in isp configuration. The 433mhz transmitter is connected to the attiny2313 over the yellow wire from Pin PD1 (TX) microcontroller to DATA(in) on transmitter module. Addionally attached a green LED on PORTD / PIN PD5 for visual debugging.

receiver circuit

img_20161103_095733

The attiny2313a is running in isp configuration. The 433mhz receiver is connected to the attiny2313 over the green wire from Pin PD0 (RX) microcontroller to DATA(out) on receiver module. Addionally attached a red LED on PORTD / PIN PD5 for visual debugging.

software

Let’s go over to the interesting part – the software. The following code for transmitter and receiver show how it is possible to remote control a LED.

transmitter

The transmitter includes the functionality to write down data packages to uart interface and send them to the receiver module. The data packages are of this form:

  1. Synchronization byte ( value hex: 0xAA – binary – 10101010 )
  2. Receiver address byte
  3. data/command byte
  4. Checksum byte (which is the sum of receiver address and transmission data)

The synchronization byte at the beginning of every telegram is used for the receiver to calculate the transmission frequency and helps to increase the transmission success. The receiver address byte is used to remote control different devices in term of several receiver modules. The transmittion data byte contains the actual data which has to be transmitted. In this example is the transmitted data a command to remote controll a led. The checksum byte is used to give the receiver the chance to proof if the transmitted data is valid.

main.c

 

receiver

The receiver has the functionality to read in the received data from the receiver module over the uart interface and proof afterwards if the transmitted data is valid.  For reading from uart interface I choose the rx-interrupt proceedings with the uart vector USART_RX_vect. The uart rx-interrupt method is called every time a new byte has been read into rx input buffer. The interesting part which contains the data validation is placed into the interrupt function.

The validation procedure looks for incoming bytes which match to the receiver address (here hex: 0x11). If an address match has been found the following two bytes are investigated more precisely. The validation is structured into two steps (STEP1 and STEP2). If an match has been found the procedure moves first to STEP1. STEP1 checks the byte after the address byte which is has to be the command (data of transmission). If this byte matches again with receiver address something went wrong in transmission and we stay in STEP1. If the receiver byte is not the receiver address the byte is saved temporary and STEP2 is active. In STEP2 is the second byte after an adress match analysed which has to be the checksum. The checksum has to be the sum of the address byte and data byte. Therefore we check here if the checksum is equal to the sum of the address byte and the cmd byte we saved temporary before. If the checksum is equal to the sum of address byte and data byte the transmission has been successfull. If the checksum is an address byte again the procedure goes back to STEP1.

main.c

 

debugging

If the red receiver led is toggled the receiver and transmitter work correctly. But if you face problems getting your devices work it can be hard to use only leds for debugging. You also have no point on how stable the transmission is or how much of your telegrams are transmitted successfully. Therefore I recommend to use an USB to serial adapter to determine if the uart interfaces of the transmitter and reicever microcontoller work corretly and to measure how stable the transmission is.

The receiver code already has the functionality to write down the successfully received commands to uart output. If you read in those commands to your computer you can count the successfull transmitted commands. While the transmitter sends every telegram 10 times in sequence you can make a statement about the transmission rate.

I wrote a little command line tool to display the succesfully received commands on the command line:

serialmonitor

While every second 8 or 9 commands are displayed my configuration has an transmission rate between 80 and 90 percentage. If you take distance between those chips the transmission rate will fall off very quickly. For long range transmission I suggest to use a lower baudrate and an external antenna soldered to transmitter and receiver.

If you dont know  the numbers displayed – 255 is in hex notation 0xFF which isthe led on command and 15 is in hex notation 0x0F which is the led off command.

If you want to have the command line tool write me by email.