Showing posts with label SPI. Show all posts
Showing posts with label SPI. Show all posts

Friday, 1 July 2022

DMX to Neopixel Arduino Shield

In a previous post (nearly a year ago!) I mentioned I was going to design an Arduino shield to allow DMX control to Neopixels.  I actually did design a board but never wrote a blog post about it.  Here is where I rectify that.


I decided to design my own DMX shield for the Arduino R3 as although there are commercial off the shelf versions available they don't have electrical isolation between the RS485 transceiver and the IO (Input Output) ports. This can be quite critical when connecting up DMX lamps as some of them are not well designed and lets just say ground loops and cheap DMX lamps becoming live when they shouldn't and releasing of magic smoke and electric shocks being a very real and present danger...don't ask me how I know... 

The circuit itself is pretty much the same as those already available.  It has opto-coupling present on the IO, the power supply and has DIP switches on board to set the DMX start address.

Here is the circuit diagram:
DMX to SPI Converter Shield

I suppose I had better explain the circuit - This is as much for me as for the casual reader...I'll be honest I haven't looked at this for a year and some decisions taken were odd to me at first...  

12 V dc input to 5 Vdc out circuit (Switch-mode)


The circuit section shows the 12 V dc input coming from the connector J1 going to C1 (100 nF) - this is a filter capacitor.  Then a DC to DC converter module is present.  Three 100 nF capacitors are then present to filter the output of the DC to DC converter.  These switching converters are known to often cause electromagnetic interference.  The capacitors are present to try to mitigate those issues.

The switching converter (PS1) used is a TRACO TEA_1-505 and it's datasheet is here: 

https://uk.farnell.com/traco-power/tea-1-0505/dc-dc-converter-5v-0-2a/dp/3465028

The next section is the opto-coupled RS485 (DMX)section:


The serial and control signals from the Arduino R3 are connected to Jumpers (JP1 to JP4).  This allows the user to isolate the connections from the serial pins of the Arduino R3 to allow for code upload and control of whether the DMX device will be active (in control of the DMX network and sending data packets) or passive (receiving Data packets).

I'm going to discuss each opto-coupler in turn to simplify things:

Resistor R1 (4.7 kΩ) is connected to output of Opto-coupler U1 and is present to current limit the signal presented to the serial input RX of the Arduino R3 (or clone).  Resistor R6 (470 Ω) is present to current limit the signal presented to the input of U1 coming from the RO output of the RS485 transceiver (U4 - MAX 481E).  


Resistor R2 (470Ω) is connected to the input of Opto-coupler U2 and is present to current limit the signal presented to the internal diode of the device.  The output is current limited by resistor R4
(4.7 kΩ) and is connected to the DE and RE (inverted) inputs of the RS485 transceiver (U4 - MAX 481E). 

Resistor R3 (470Ω) is connected to the input of Opto-coupler U2 and is present to current limit the signal presented to the internal diode of the device.  The output is current limited by resistor R5
(4.7 kΩ) and is connected to the DE and RE (inverted) inputs of the RS485 transceiver (U4 - MAX 481E). 

The Opto-Couplers are powered by 5 V dc coming from the regulated Arduino R3 supply and are isolated from the 5 V dc signal coming from the DC to DC converter 

The IO (A and B) signals of the R485 transceiver (U4 - MAX 481E) are connected to three resistors (R7 - 562 Ω, R8 - 133 Ω, and R9 - 562 Ω).  These are present to provide the 120 Ω impedance matching for the RS485 transceiver.  The input and output to the RS485 transceiver are connected to screw terminals with an isolated return (GND2).  These will connect to the signal cable used to connect this circuit to the DMX controller. 


The next section is present to allow the user to pre-set the DMX address:


The 8 way DIP switches (SW1) are connected to the Arduino R3 spare pins and 330 pull up resistors.  It is a standard way of connecting switches to microcontroller input pins.  The current presented to the microcontroller inputs when the switch is closed is 15 mA which is within the specification of the device (ATMEL 328).

The SPI output of the circuit is taken from Pin 12 of the Arduino R3 along with supply voltage and ground to a three terminal screw connector.  This is a nice robust way of connecting to off the shelf Neopixel circuits. 

The final section is the standard layout used for connecting a shield circuit board to an Arduino R3.


It saves time designing PCB layouts as all of the dimensions and connections are present and contain NET labels.

Here is the bill of materials in case it is needed:


I haven't ordered any of these parts yet but I already know that some are not in stock...the fallout from the pandemic is very real.  Some are due in next week so I can get ordering!

Here is the PCB layout: 


The 3D render is probably easier to see and understand:

The DMX to SPI Converter shield - Top Layer 

The DMX to SPI Converter shield - Bottom Layer

There isn't much to say about the PCB layout.  I've tried to make sure that all of the traces carrying high current are nice and thick. The silk screen labels are visible and it is fairly easy to populate by hand if required.  I wish I had labelled in the inputs and outputs so I know where the GND and +12 V input connections are and where the DMX and NeoPixel connections are.  There are always things I would do differently if given a chance to repeat.

I got ten boards made by PCBWay for a reasonable price and they were delivered in very short order!

Here is a picture of the unpopulated board:


My plan is to populate and test this board as soon as possible as I intend to use it in my next project - the DMX controlled patio lamp :)

That is all for now - take care always - Langster!

Tuesday, 14 January 2014

mbed SD Card tutorial

I have been playing with the mbed microcontroller again.  I'm really liking this particular microcontroller development platform!  The potential is huge for what can be achieved with this thing.  The only thing I continually struggle with is the documentation.  Whilst the mbed website is good there are still issues that cause the inexperienced to become frustrated...myself included!

One of the most common tasks I need a microcontroller to perform is to log data from instrumentation.  The best way I know of currently doing that is to use an SD card.  I have done this with an arduino so often I almost can do it without thought.  At the moment I am developing some instrumentation with the mbed and need to achieve the same thing - I need a way of logging data measurements.  The data will be stored as text files in ASCII.  I will probably use comma separated values....It makes it easy to import into a spreadsheet or graphing program of choice.

If you are going to try this you will need the following:

An mbed and mini USB cable
An SD card - partitioned and formatted for use on your operating system of choice
An SD card breakout board - try Adafruit or Sparkfun for all your breakout needs!
Some connection wire
Patience!



So I looked at the mbed documentation on the website and searched for SD card tutorial.  A lot of pages appeared but nothing that actually shouted this is how to connect an SD card, here is the circuit diagram and how to connect it up and here is the demonstration code with an explanation of how things work....

https://mbed.org/search/?type=&q=SD+card+tutorial - a lot of pages but nothing definitive

What I actually needed to find was here:


The mbed cookbook is useful but often difficult to navigate - a work in progress.

There is a lot of information on the programming of the mbed - libraries and usage but no pictures on how to connect things up and no schematics.  Lets remedy that!


SD cards normally communicate with the host microcontroller via the SPI protocol - Serial Peripheral Interface.  The mbed has two SPI ports - pins 5, 6 and 7 - MOSI, MISO and SCK and pins 11,12 and 13 - also MOSI, MISO and SCK.  You can use either three pins but they are not interchangeable.  In my testing I used 11,12 and 13 because I have already made plans for pins 5 and 7!

Here is a picture of the connections:


I used an SD breakout board so my connections are a little different from the above schematic.  I am connecting +5V to Vdd because my breakout board has a logic level converter to convert 5V to 3.3V.  The breakout board was designed for use with TTL logic based microcontrollers like the arduino.  If you are using just an SD card connector the information in the schematic is correct.  Change your connections to suit your breakout board as necessary.

Once I had checked and rechecked my connections!  I keep having to do that at the moment - I imported the example code into the online compiler and then uploaded it to my mbed

Here is the the link to the example code again so people can find it easily:


It should compile with no errors.  Ensure that you update the code with the relevant information with where you have made the connections.  If like me you are using pins 11, 12 and 13 for MOSI, MISO and SCK you need to change the line highlighted to reflect that.

#include "mbed.h"
#include "SDFileSystem.h"

SDFileSystem sd(p11, p12, p13, p8, "sd"); // the pinout on the mbed Cool Components workshop board

int main() {
    printf("SD card test!\r\n");   

    mkdir("/sd/mydir", 0777);
    
    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
    if(fp == NULL) {
        error("Could not open file for write\r\n");
    }
    fprintf(fp, "Hello fun SD Card World!\r\n");
    fclose(fp); 

    printf("File successfully written and closed!\r\n");
}

I suppose I should try and explain what the code does:

The first line tells the compiler to include the core header file for the mbed and the second include statement tells the compiler to use the header file containing the instructions relating to SD cards.

The third line SDFileSystem sd(p11, p12, p13, p8, "sd");  tells the compiler that the SD card is connected to pins 11, 12 , 13 and pin 8. 

NB - Pin 8 is the control line for the SD card it's known as CS or SS depending on which information you read or what is printed on the breakout boards silk screen - mine was SS.

The rest of the code prints a message to the mbed com port, makes a directory on the SD card and then creates a text file called sdtest.txt.  If for some reason the file cannot be created or opened an error message is displayed on the serial terminal; the program then finishes.  If the file was created and opened successfully a message will be written inside the text file and then the test file is closed. A further message is sent to the serial terminal to document the program's success.

I use a free terminal program called PuTTy for my serial, Telnet or SSH requirements.  It is a great piece of software!  You can get it from here:


I suggest people try it out - it's useful...

Anyway...back to the cool stuff.  Connect up your mbed to your PC and copy the compiled .bin file to your mbed and press the reset button.  If all is well the blue power / programming LED will flash and then the program will exit.  No other LEDs should be illuminated.  

If you load up PuTTy and point it towards your mbed serial terminal with the settings:

COMXX - 9600, n, 8, 1 with no flow control - where XX is the COM port number of your mbed you can view the output:


Open the terminal window and view the results...


Actually that was amazing for me...I couldn't get this to work for love nor money at first - I'm losing my touch!

If you remove your SD card and view it in a file explorer there will be a new directory on your SD card called mdir and inside this folder there will be a text file called sdtest.txt.  Open it and verify it is all correct.

If things are wrong and they often are...then the four LEDS at the bottom of the mbed will flash from outer to inner and the following messages will be shown in PuTTy:


The flashing LEDS of SD card failure...


If you see this then unfortunately something is wrong either with the wiring, the SD card or your mbed.  In my case I was struggling for a long time because I was using the unregulated +5V input to power the SD card.  My computer couldn't power both the mbed and the SD card.  As soon as I swapped to the regulated +5V everything worked perfectly.  If it doesn't work for you, all I can suggest is check check and re-check connections and then ask for help!  I didn't but perhaps I should have...I even checked the MOSI, MISO and SCK where occurring using an oscilloscope and a logic analyser.  I re-wired at least ten times and checked continuity with a multimeter.  It wasn't until I measured the voltages with a digital multimeter that I noticed that there was not enough voltage to power the SD card.  If I had been using a breakout board without a logic level converter and wired as shown in the schematic diagram this would have worked first time....No matter - all is working and well now and more information and confidence gained - bonus!

There are so many reasons to want to log data to external memory with microcontrollers - it's the corner stone of instrumentation.  I'm going to turn this into a memory stick of sorts so that I just have to connect a USB cable to the mbed (not the programming cable) so that I can browse the SD card data!  Simples!

That's all for now people - take care

Langster!