Receive DMX-512 with an Arduino

Shiny and new out of the box!
Prologue: For Christmas, I received an Arduino. If you’re not familiar with them, they’re like a little computer with a lot of pins to which you can connect outputs like LEDs, servos, relays, triacs, or anything you’d want to control, as well as photosensors, switches, anything you’d want to take an input from. You write your program in the easy-to-learn Arduino environment, upload it to the Arduino board, and it’ll run your program automagically. I’m not a programmer, but less than an hour after taking it out of the box I had it blinking an LED for me. Buy one, they’re perfect for all of us who are trying to create some Theater Magic with no money or hope of getting any.
Well, Almost Perfect. There’s been a way to send DMX with an Arduino for awhile, but when I started poking around for DMX reception code, I came up with zilch. If you’re already savvy with microcontrollers and assembly code and avrdude and whatever-the-fuck-else, you probably know about this solution. Me, I look at assembly code and I just hear a dull screaming in my head, nevermind all that other stuff that I don’t know how to do either.
So I figured that a great first project would be to remedy this situation, and write a program to receive DMX on the Arduino platform. In the way of all Works in this Vale of Tears, this ended up being much more difficult and taking much longer than I initially anticipated. But eventually I figured it all out, and so here it is!
Features:
- In-the-field addressing from 1 to 512 via two tact switches (works with the previously released I/O Shield, here).
- Address is stored in non-volatile EEPROM, so it is retained when power is lost to the Arduino.
- Addressing hardware allows full use of the pins.
- Number of addresses to receive is configurable.
- Works with controllers that send less than the full 512 address set.
- Break detection is done correctly by detecting a Low value of >88μS per ANSI E1.11-2008, rather than the frame error hack used by many devices.
- Uses interrupt-based subroutines to eliminate processor-load related timing problems.
- If the DMX data signal is lost, the Arduino will maintain the current state until new values are received.
- The reception and user code run sequentially rather than at the same time, so they won’t interfere with each others’ timing.
You Will Need:
- A copy of the latest release and the modified wiring_serial.c or HardwareSerial.cpp file. See the instructions for what to do with the files. *update*: This post is now more than four years old. Some people have reported success using the 1.0+ software, but I’d start with the older rev 0023 of the IDE and go from there once you get it working. Download it here, under Previous IDE Versions.
- An Arduino with an Atmega168 or Atmega368 processor. Because of the timing-sensitive nature of DMX-512, some of the code had to be optimized by referring to particular registers on the Atmega168/368. The code can easily be adapted to other processors, though.
- An RS-485 to Serial Transceiver, such as the MAX485 or the TI 75176. They’ll run you about $1, the MAX485 is a little more but it apparently has some kind of fancy overvoltage protection, so I used that.
- A breadboard and some wires, also a 150Ω resistor to terminate the DMX line if necessary.
Instructions:
DMX uses a twisted pair of signal wires with opposite polarity to transmit information per RS-485. However, your Arduino needs a serial signal, with a pin brought high for one and low for zero. To convert between these protocols, you’ll need to wire up your MAX485 or 75176 in the following way:
Here are some pictures which may help you:
Note 1: Starting with Rev11 of the software, I adjusted the pin layout slightly for better routing on the DMX I/O Shield. The two gray wires in the above photos that are plugged into pins 3 and 4 should be plugged into pins 2 and 3, respectively, if you’re using the latest software.
Note 2: I’m using the Arduino protoshield here, which I highly recommend, they’re handy. If you don’t have one, the pin layout is the same as if you ran directly into the Arduino board.
Note 3: if you want to retain the use of pins 1, 4, and 5, at the cost of being able to transmit as well as receive, you can connect the corresponding pins on the MAX485 to the ground on the Arduino board. I’ve done it this way for possible future RDM functionality ;).
One Dumb Hack is necessary: rename your currently installed HardwareSerial.cpp file to HardwareSerial.cpp.backup, and put the modified HardwareSerial.cpp from this site in the same directory. It’s located in:
(Arduino Install Directory)/hardware/cores/arduino/
The reason for this is that the Arduino software defines the USART_RX_vect serial reception interrupt, and includes it in your compiled code, even if you don’t use any serial functions. This will, I hope, be fixed in a future release, but until it is this is the work-around. You can read more about the issue here. Once you’ve uploaded the sketch and it’s working to your satisfaction, you can undo this part to regain your normal serial library functionality.
Finally, fire up the Arduino software, and put what you want the Arduino to do with the received values in the action() loop. The received values are stored in the dmxvalue[] array. The downloaded sketch contains example code to print out each of the values to the serial port, and set PWM pins 5 and 6 to the first and second value in the array, respectively, but this can of course be changed to anything you want.
That’s it, let me know how it works for you! You may want to keep reading for the Known Limitations, etc.
Known Limitations:
- Atmega168 and Atmega328 based processors only (you will have to rename the registers and interrupt vectors if you want to use it for another processor).
- I personally only test the software with a USB-DMX Pro controlled via Lightfactory, since that’s what I have laying around. But it handles a variety of frame rates, break lengths, etc. just fine. The consensus seems to be that it works just fine with other controllers.
- Because I needed access to exact timing, I had to use the Timer2 functionality, so pins 3 and 11 cannot be used for PWM. The Timer2 issue is also the most frequent cause of incompatibility problems when using community-developed libraries. See the comments for remedies if you’re affected by this.
- Will not detect bad addressing. For example, with it set to receive the default 8 channels, “dmxaddress=510;” would give you two good channels and six channels of junk. Or ”dmxaddress=50;” when only 55 addresses are sent by the controller. Edwin Dolby at Laser Productions had an elegant idea to address this, namely that you could use the constrain function to map out of bounds values to the correct 0-511 range. However, I have decided not to implement this by default, as without some kind of numerical readout I think the values should just be set to what you set them. But, easy to implement if you decide you want it!
When addressing, sometimes when you hit the 0 or 1 switch it doesn’t take. I’ve programmed the LED to turn off briefly if the bit was successfully entered, so if you don’t see it go off, you’ll have to hit the switch again until it takes. I don’t know why it’s doing this, if you have some time to wade through the logic let me know why and I’ll update the code.Ron Barber was good enough to figure out why it was doing this and show me how to fix it. Thanks Ron!
Future Development:
- Add a dip switch and code to allow in-the-field addressing.
- Reduce the number of Atmega168-specific functions to improve code portability.
- Design a shield for better durability, signal pass-through, termination, etc. Check here!
- Add frame error and data overrun error handling routines.
- Add RDM functionality.
- Develop a separate DMX monitor application in Processing or the like.
- Timer2 is currently used for break detection. I’m thinking that I could re-write the code so that it records the configuration register values currently existing, sets them to what it needs, and then puts them back the way it found them. That way, you would be able to use your other libraries that rely on this timer (of which there are apparently a lot), as long as they didn’t need to run during break detection. Advantages and disadvantages to this, obviously. But the #1 issue seems to be ‘doesn’t work with library X’, so for most of you this would be an improvement. Or a polling loop might even be good enough, and then we could forgo the timer business altogether.
- The documentation for this project has sprawled out onto so many posts and comments that it’s difficult for even me to find, I’m looking at trying to consolidate everything into a minisite or something. I’d also like to add a big ‘how it works’ section, which would help those of you trying to customize the code.
Release History and Notes:
- 9 October 2010: Rev15.
- Tested and working with IDE version 0021. I have not tested it with the new Arduino Uno hardware, but can’t think of any reason why it wouldn’t work. If you have any success or otherwise with the new hardware, drop a line in the comments.
- A bug has been fixed in the addressing routine caused by button debounce, and the addressing routine logic has been simplified and made clearer. Big thanks to Ron Barber for pointing out the source of the bug and contributing new code.
- There is now a check when the previously stored address is read from EEPROM to ensure that it is in the valid range of 1-511, to prevent a bad value being read in from uninitialized EEPROM.
-
Fixed a potential bug in the break detection routine that could cause the read values to be off by one in cases where the break from the microcontroller was exactly 88uS.
- 23 June 2010: Rev14
- Tested and working with IDE version 0018.
- 9 July 2009: Rev13
- Tested and working with IDE version 0016.
- The number of channels to receive is now easily user-configurable.
- Replaced static variables with #define statements for memory optimization (+48 bytes, woot!).
- 12 May 2009: Rev12
- In-the-field addressing via two tact switches (works with the previously released I/O Shield, here).
- Address is stored in non-volatile EEPROM, so it is retained when power is lost to the Arduino.
- Addressing hardware allows full use of the pins (which is why I didn’t use the more conventional dip switch setup).
- Some of the variables were localized, since the sketch is now getting pretty complex.
- 27 April 2009: Rev11
- Adjusted pin layout for better routing on the DMX I/O Shield. Pins 3 and 4 in Rev10 are now pins 2 and 3, respectively.
- 1 April 2009: Rev10
- Cleaned up and improved code commenting.
- Adjusted HardwareSerial.cpp (included) so the code will compile on Arduino software release 0015. If you’re still using 0014 or 0013, you’ll replace wiring_serial.c instead (also included).
- Replaced manual register configuration of the USART with the Arduino function serial.Begin(250000), which apparently works just as well and reduces the number of Atmega168-specific register calls considerably.
- Moved the action() loop (what you want the Arduino to do with the received values) to its own tab, to make the code easier to use.
- 20 March 2009: Rev09
- First release
- Rev00-Rev08: Pre-release betas.
The Code: Here is the .pde sketch file. It may be of general interest as well if you’re trying to write interrupt-based programs for the Arduino. In the download, there is also a tab for the user code and another for in-the-field addressing module.
[cc]
/***********************************************************
* DMX-512 Reception *
* Developed by Max Pierson *
* Version Rev15 9 Oct 2010 *
* Released under the WTFPL license, although I would *
* appreciate Attribution and Share-Alike *
* See blog.wingedvictorydesign.com for the latest version. *
************************************************************/
/******************************* Addressing variable declarations *****************************/
#include <EEPROM.h>
#define NUMBER_OF_CHANNELS 8
//the number of channels we want to receive (8 by default).
#define SWITCH_PIN_0 11 //the pin number of our "0" switch
#define SWITCH_PIN_1 12 //the pin number of our "1" switch
unsigned int dmxaddress = 1;
/* The dmx address we will be listening to. The value of this will be set in the Addressing()
* function and read from EEPROM addresses 510 and 511.
/******************************* MAX485 variable declarations *****************************/
#define RECEIVER_OUTPUT_ENABLE 2
/* receiver output enable (pin2) on the max485.
* will be left low to set the max485 to receive data. */
#define DRIVER_OUTPUT_ENABLE 3
/* driver output enable (pin3) on the max485.
* will left low to disable driver output. */
#define RX_PIN 0 // serial receive pin, which takes the incoming data from the MAX485.
#define TX_PIN 1 // serial transmission pin
/******************************* DMX variable declarations ********************************/
volatile byte i = 0; //dummy variable for dmxvalue[]
volatile byte dmxreceived = 0; //the latest received value
volatile unsigned int dmxcurrent = 0; //counter variable that is incremented every time we receive a value.
volatile byte dmxvalue[NUMBER_OF_CHANNELS];
/* stores the DMX values we're interested in using--
* keep in mind that this is 0-indexed. */
volatile boolean dmxnewvalue = false;
/* set to 1 when updated dmx values are received
* (even if they are the same values as the last time). */
/******************************* Timer2 variable declarations *****************************/
volatile byte zerocounter = 0;
/* a counter to hold the number of zeros received in sequence on the serial receive pin.
* When we've received a minimum of 11 zeros in a row, we must be in a break. */
void setup() {
/******************************* Max485 configuration ***********************************/
pinMode(RECEIVER_OUTPUT_ENABLE, OUTPUT);
pinMode(DRIVER_OUTPUT_ENABLE, OUTPUT);
digitalWrite(RECEIVER_OUTPUT_ENABLE, LOW);
digitalWrite(DRIVER_OUTPUT_ENABLE, LOW); //sets pins 3 and 4 to low to enable reciever mode on the MAX485.
pinMode(RX_PIN, INPUT); //sets serial pin to receive data
/******************************* Addressing subroutine *********************************/
pinMode(SWITCH_PIN_0, INPUT); //sets pin for '0' switch to input
digitalWrite(SWITCH_PIN_0, HIGH); //turns on the internal pull-up resistor for '0' switch pin
pinMode(SWITCH_PIN_1, INPUT); //sets pin for '1' switch to input
digitalWrite(SWITCH_PIN_1, HIGH); //turns on the internal pull-up resistor for '1' switch pin
/* Call the addressing subroutine. Three behaviors are possible:
* 1. Neither switch is pressed, in which case the value previously stored in EEPROM
* 510 and 511 is recalled,
* 2. Both switches are pressed, in which case the address is reset to 1.
* 3. Either switch is pressed (but not both), in which case the new address may
* be entered by the user.
*/
//set this equal to a constant value if you just want to hardcode the address.
dmxaddress = Addressing();
/******************************* USART configuration ************************************/
Serial.begin(250000);
/* Each bit is 4uS long, hence 250Kbps baud rate */
cli(); //disable interrupts while we're setting bits in registers
bitClear(UCSR0B, RXCIE0); //disable USART reception interrupt
/******************************* Timer2 configuration ***********************************/
//NOTE: this will disable PWM on pins 3 and 11.
bitClear(TCCR2A, COM2A1);
bitClear(TCCR2A, COM2A0); //disable compare match output A mode
bitClear(TCCR2A, COM2B1);
bitClear(TCCR2A, COM2B0); //disable compare match output B mode
bitSet(TCCR2A, WGM21);
bitClear(TCCR2A, WGM20); //set mode 2, CTC. TOP will be set by OCRA.
bitClear(TCCR2B, FOC2A);
bitClear(TCCR2B, FOC2B); //disable Force Output Compare A and B.
bitClear(TCCR2B, WGM22); //set mode 2, CTC. TOP will be set by OCRA.
bitClear(TCCR2B, CS22);
bitClear(TCCR2B, CS21);
bitSet(TCCR2B, CS20); // no prescaler means the clock will increment every 62.5ns (assuming 16Mhz clock speed).
OCR2A = 64;
/* Set output compare register to 64, so that the Output Compare Interrupt will fire
* every 4uS. */
bitClear(TIMSK2, OCIE2B); //Disable Timer/Counter2 Output Compare Match B Interrupt
bitSet(TIMSK2, OCIE2A); //Enable Timer/Counter2 Output Compare Match A Interrupt
bitClear(TIMSK2, TOIE2); //Disable Timer/Counter2 Overflow Interrupt Enable
sei(); //reenable interrupts now that timer2 has been configured.
} //end setup()
void loop() {
// the processor gets parked here while the ISRs are doing their thing.
if (dmxnewvalue == 1) { //when a new set of values are received, jump to action loop...
action();
dmxnewvalue = 0;
dmxcurrent = 0;
zerocounter = 0; //and then when finished reset variables and enable timer2 interrupt
i = 0;
bitSet(TIMSK2, OCIE2A); //Enable Timer/Counter2 Output Compare Match A Interrupt
}
} //end loop()
//Timer2 compare match interrupt vector handler
ISR(TIMER2_COMPA_vect) {
if (bitRead(PIND, PIND0)) { // if a one is detected, we're not in a break, reset zerocounter.
zerocounter = 0;
}
else {
zerocounter++; // increment zerocounter if a zero is received.
if (zerocounter == 20) // if 20 0's are received in a row (80uS break)
{
bitClear(TIMSK2, OCIE2A); //disable this interrupt and enable reception interrupt now that we're in a break.
bitSet(UCSR0B, RXCIE0);
}
}
} //end Timer2 ISR
ISR(USART_RX_vect){
dmxreceived = UDR0;
/* The receive buffer (UDR0) must be read during the reception ISR, or the ISR will just
* execute again immediately upon exiting. */
dmxcurrent++; //increment address counter
if(dmxcurrent > dmxaddress) { //check if the current address is the one we want.
dmxvalue[i] = dmxreceived;
i++;
if(i == NUMBER_OF_CHANNELS) {
bitClear(UCSR0B, RXCIE0);
dmxnewvalue = 1; //set newvalue, so that the main code can be executed.
}
}
} // end ISR
[/cc]
P.S.: There’s a really good comment thread for this post and it may answer your question, especially if it is of the form ‘will it work with this other Arduino library?’ So take a gander at that before writing a comment, if you would.



September 27th, 2011 at 6:11 am
i can confirm that the shield must be unplugged to upload code. on my boards i put a jumper on pin0, or use a 10way dip switch for adressing and use the extra pin to enable/disable the 485 transciever.
to the people who requested my modifications to Max’s original (and fantastic) code:
i have recently updated my code for a new project, i’ll post it on my website in the next few weeks.
now it can use adresses 1- 511,
3 channels of pwm with p-channel mosfets
and 3 240v relays!
Max, i have one problem though, if i disconect the dmx cable, it receives random gibberish.(not before connecting though…)
ewan.
does mark after break detection only trip once?
January 3rd, 2012 at 6:53 am
Hi
is there a way to get this working in Arduino 1.0 I have got some trouble to get this working I just wan´t to get my Arduino Bord to recive DMX. There isn´t any other solution on that I think. I think it´s the crappy HardwareSerial.cpp thing that I stuck on
Many thanks for your Help.
Sebastian, Hannover Germany
January 3rd, 2012 at 12:57 pm
Hi Sebastian,
I’m sure this is possible, my prior experience has been that the HardwareSerial.cpp changes little if at all from release to release, so you just need to find the lines that conflict and comment them out. But I’ve got so many other projects in the works these days that I wouldn’t expect I’d get to this any time soon. In the meantime, unless there’s some new aspect of the software that you need, you could always use a prior tested release to compile.
Max
January 3rd, 2012 at 3:04 pm
Many Thanks,
I hav got the clue by using the 0021 release of Arduino no errors while compiling. The new HardwareSerial.cpp seems to be completely different to me 200 lines to 400 lines … I now ran into other cruxes – I opend the three files on arduino and it seems to me that only one sketch is uploading to the board. Maybe you could give me a hint …
January 4th, 2012 at 1:03 am
Hi again,
it´s working now ! Thank You for your great job ! I testet it with my Open DMX USB with some kind of DMX Software. Some of my desks dosn´t working in fact they send 512 chans.
MA 12/02 / grandMAUltraLight / LeapFrog 48
January 10th, 2012 at 10:32 am
Sebastian H: any chance you could post your solution to the 1.0 dilema? I’m having the same problem.
January 12th, 2012 at 7:01 am
Just a quick question: will this work on the Mega so I can use the other serial ports to communicate over RS232?
January 17th, 2012 at 5:26 am
@Bennage
Hi, I just use the older Release Version 0021
January 20th, 2012 at 8:22 am
i could not find the .cpp so just to anyone that have the same problem i found mine under \arduino-0022\hardware\arduino\cores\arduino
February 3rd, 2012 at 3:00 pm
Hi, I have a little problem with the DMX code. When I connect the board to the PC through a usb-DMX cable and use the DMX program it works fine but when I connect it to a 6 ch DMX pannel the channels are overlaping and the period between channels fluctuates.
The signal from the PC doesn’t do that.
So the question is how I can make it work with that DMX 6 ch pannel?
February 3rd, 2012 at 3:03 pm
I forgot to mention! Very good job! and Thanks! for the code.
February 4th, 2012 at 1:33 pm
Christoffer, I think i am just being very dumb right now, but I don’t know how to access any of these files, my computer doesn’t seem to think they exist, any tips on how to get to them?
February 9th, 2012 at 3:46 am
I’ve been using the shield to receive and transmit today- very cool. If anyone needs help with transmission- I’ve been using the DmxSimple code from http://code.google.com/p/tinkerit/wiki/DmxSimple
I found that with this shield you need to add “const int RS_ENpin = 3;”
then add a line that writes that pin HIGH to enable transmission
I also found that you want to “DmxSimple.usePin(1);” use pin 1 for transmission.
It works and I’m just looking for more to do with the setup now!
February 15th, 2012 at 5:35 pm
Hi teams, please help, i’m need control 8 outputs for my neoon streming lighting,for customs dressing’s distribuit in 8 ch. outputs, my idea is control from my dmx wireless a 8 outputs.
please let me know how to find the correct cirucuit board i’need for that???
thanks in advance.
Regrads.
Roberto Bento
RB Lighting Design
March 19th, 2012 at 8:59 pm
I need to covert DMX to serial to transmit wirelessly via Xbee…will this method work?
April 20th, 2012 at 12:36 am
Hey Max,
awesome job!! I’m making a DMX to Analog converter for 18 channels. I’m using MAX5590 (SPI DAC) followed by an opamp stage of gain 2 to get 0-10v output. Is there sufficient time between the DMX packets to shift the data out via SPI?
May 16th, 2012 at 8:05 am
Hi Deepak,
Once you receive new DMX values, you can take all day to execute the code that does whatever you’re doing with the received values. The only question is whether the Arduino SPI library uses Timer2, in which case you would have to rewrite my code, or theirs, to use a different timer. See earlier in the comment thread for a discussion of this issue. Thanks!
May 19th, 2012 at 8:53 am
I’m very new to Arduino but, as I’m a lighting man, I want to play with DMX as a learning exercise. I have a Uno and have been reminding myself how to program in C and now want to use Max’s software. Are there any step by step instructions for getting it to run on the Uno? I get lots of compile errors.
May 24th, 2012 at 10:10 pm
Thank you for making this public. Using your code (slightly modified) and schematic, I developed a seven voice synthesizer controlled entirely by a lighting console. A demonstration of it can be found here: http://www.youtube.com/watch?v=G4FR4_NgrNM&feature=relmfu. Thank you once again.
June 6th, 2012 at 1:19 pm
Hey DrV,
As regards compiling this program on Uno/Arduino 1.0, there have been some reports of success. If you read through the comments, they may give you some tips. I’d like to update it to work with the new software/hardware, but simply put I don’t have a Uno hardware, and I’m not planning on buying one because I have several prior-generation Arduinos and so I’m set for the moment. But if someone wants to give me a Uno or loan me one, I’ll put together an update.
Sorry for the inconvenience,
Max
October 20th, 2012 at 12:33 pm
Hi Dear All,
Someone did this project using Ethernet or WI-FI Shield?? I really want do one similar.
Best Regards,
November 6th, 2012 at 10:50 pm
Hello Dear All,
I’d like to receive DMX values over Serial port with this code, using LightsUp! or Freestyler software. Can anybody help with the code, please? I tried this “http://www.mathertel.de/Arduino/DMXSerial.aspx” but when I use library “BlinkM_funcs.h” for controlling BlinkM LEDs (http://thingm.com/products/blinkm) I get errors. :-(
November 15th, 2012 at 2:25 am
followed exactly what was said by you but get compilation “core.a (HardwareSerial.cpp.o): In function` __vector_18 ‘:
C: \ Documents and Settings \ NicolaeMuntean \ Desktop \ uno dmx \ arduino-1.0.1 \ hardware \ arduino \ corresponding \ arduino / HardwareSerial.cpp: 100: multiple definition of `__vector_18 ‘
receiver_rev15.cpp.o: D: \ Temp \ build51381444179118878.tmp/receiver_rev15.cpp: 162: first defined here
November 15th, 2012 at 2:38 am
waw! I think I am wrong uno card and when I checked did never give mega error. Can be modicficat software for uno?
November 19th, 2012 at 10:13 am
Anyone tried to create this part:
-Develop a separate DMX monitor application in Processing or the like.
I started with this, but noticed my processing skills are rather rusty so maybe someone already gave it a try before?
Also, is it possible to still use the arduino Serial print functions with the Max485 chip connected to the Rx/Tx ports?
November 28th, 2012 at 7:01 pm
Where’s the resistor and whats it for. I know there is a couple on the shield to transmit. Is it just the opposite of that board…whats the deal
January 21st, 2013 at 2:12 pm
Anyone have any luck with this and a wireless shield or an xbee?
Anyone update this to the new software?
April 5th, 2013 at 12:58 pm
hi. thanks for the great work.
i am trying to get this running on a arduino pro mini from sparkfun.
5v 16mhz atmega328p
so far no luck.
i am using IDE 1.0.4 and commented everything out in hardwareserial.cpp
from
until
#if !defined(USART0_RX_vect) && defined(USART1_RX_vect)
#if defined(USART1_RX_vect)
i am uncertain if i have to use
ISR(USART_RX_vect) or ISR(USART0_RX_vect)
UDR0 or UDR1
etc?
thanks for any advice
April 6th, 2013 at 5:43 am
ok now it works:
USART_RX_vect
UCSR0A
UDR0
FE0
and since i am using pins 0 and 1 i had to not connect the fdti adapter but rather power the board through the raw pin.