Tuesday 2 January 2024

Plotting Data and performing integration using a spreadsheet program

I haven't written a post in ages...my apologies but I rarely have time these days or anything of interest to post about...my work has taken over. I hope to get back to writing and doing more things for myself but I have no idea when.  I suspect I will have to make myself get back into blogging.  It is useful as an aide memoire to myself if nothing else.  

A colleague asked me to plot some sensor data for him in a spreadsheet program and perform some analysis on it and to be completely honest I had forgotten how.

So I did some quick research and here is an example of what I did so that I can refer to it next time...It may be useful to someone else so I'm writing it up as a post.

Sensor data is collected in various ways but is normally saved to a text file in comma separated values.  

Here is an example of some comma separated value data:

sample,measurement1,measurement2
0,0,0
1,1,1.12
2,2,2.05
3,3,3.01
4,4,3.95
5,5,5.05
6,6,6.02
7,7,7.08
8,8,7.99
9,9,9.04
10,10,10.02

I made the data up as I cannot share the actual data that was discussed.

The first column is the sample No.

The second column is the current measurement in mA

The third column is the voltage measurement in volts

In case anyone is wondering these are supposed to be measurements from a simple circuit to find an unknown resistance.

A constant current power supply has been used to supply energy to an unknown resistor;  The voltage and current in the circuit were measured and recorded and the results are in the csv text file as shown above.

Here is the circuit:

Here is the tabulated measurement data:

Here is the data plotted as a line graph:


We can calculate the resistance and plot that as well...

So the Resistance (from Ohms Law) is equal to (measured volts / supplied current) * 1000

I have added a column to the table:
We can now plot this data as well if we wanted:



If we like we can get the spreadsheet program to compute the mean average resistance:

For completeness we can calculate the mean average by performing an average calculation:

The value is 1016.58 Ohms - I wish LibreOffice Calc gave me a way of easily displaying the value on the graph...I used the AVERAGE function to calculate it.

Returning to the first graph we can also use the spreadsheet program (Libre Office Calc) although Excel also has this functionality; to calculate the power consumed.

This is achieved by adding a polynomial trendline to the graph in question and then from the trendline performing integration to calculate the area under the curve which is equal to the power consumed in Watts.  Helpfully the trendline function displays the integral for us!  It has been a long time since I had to use integration and possibly the only time I have actually ever had a practical reason to apply it.  My university education wasn't a waste!
 
The function has been found to be: 


If we integrate this we have:



If we apply limits we have:




 If we then calculate out the integral applying the limits we get:



Which gives a value of 40.46...

We need to account for the fact that the current was in milli-amps:

Power (Watts) = Amps * Volts

so we need to divide our solution by 1000 giving:

Power (Watts) = 0.04

A simple check using the tabulated data finds this to be correct:
Now I'm aware in this example we could have used the (base * height) / 2 method to achieve the same result...however when we have a graph that isn't linear (straight line) as the example then the integration method is the best solution.

There is also the trapezoidal method which could be performed on the data itself but that is for another post.

I may yet write a python script which reads in the CSV data, performs the above plots and then performs the integration for me...It may be a useful exercise but that is also for another post!

That's all for now...take care - Langster! 




















Monday 4 July 2022

NeoPixel Circle PCB

The next step in the DMX patio lamp is to create a PCB with the neopixels on it.

The previous posts for reference are:

https://langster1980.blogspot.com/2022/07/designing-dmx-controlled-patio-light.html

https://langster1980.blogspot.com/2022/07/dmx-to-neopixel-arduino-shield.html

I am going for 32 LEDS but I may change my mind. It depends really on the size and spacing achievable as well as current draw and voltage drop.  Lets see what works first.

I'm confident I could design this PCB straight away but it is always a good idea to read through the datasheet and do some calculations...there may be something critical I have missed or didn't know.  I've used neopixel tape several times but I've never really bothered to read up on their technical aspects.

Here is the datasheet: https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf 

The datasheet isn't the worst I've looked at but it isn't the best either...

After some reading...and some more reading here is what I have found:

The package contains 3x LEDS and a control circuit.  According to the research the control circuit draws
8 mA with all the LEDS not active (Off). 

The Red LED draws 13 mA when fully on. 

The blue LED and green LEDS also draws 13 mA when fully on.

Therefore each pixel (3x LEDS in one package) draws 60 mA.  

If we have 32 pixels our current draw with each LED fully on (White colour) will be:

So if the calculations are correct...then we need to account for this 1.6 Amp current draw on our PCB layout. Our connector and wiring also need to handle 1.6 Amps - I'm going to design for 2 Amps to provide a little margin.

Here is the circuit diagram:
NeoPixel Lamp Schematic

Next we need to design a PCB layout.  I'm going for a circular PCB which will fit behind the 84 mm polycarbonate cover.  Lets set the diameter of the PCB to 80 mm - that way I know it will fit with room to spare. I will need a mounting option too...not worked that out yet!  I suspect some stand offs and attached to the cover will work fine.

Here is the PCB layout:

NeoPixel Lamp Top Layer - PCB Render


NeoPixel Lamp Bottom Layer - PCB Render

I added an extra pixel in the centre of the PCB as there was room.  I also added some mounting holes which weren't on the schematic.  The layout went quite well and only took me a couple of hours...must be getting better at this although it isn't because of practice!  I did have a nights sleep between the hours...maybe that helped...

I have exported the gerber files...next job is to get a quote from JLPCB and then assuming the price is right get some boards made.  I'm going to go with black silkscreen.  

I think that's all for now - take care, Langster!

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!

Designing a DMX controlled Patio Light with Neopixels

A friend of mine has approached me to make him some Patio lights. He wants them to be interactive! I'm thinking the best idea would be to make him some sort of DMX controlled light with WS2815 LEDS. I can build on the previous design work I have already done which should save me some time. 

For the enclosure my plan is to take an existing garden rock lamp and re-engineer it for this purpose. This should save me having to design some clever aesthetics. To that end I have bought a cheap (£3.20) garden lamp from B & Q - A popular Home / garden improvement chain in the UK: 


The lamp itself looks like this:
   
Simple Garden Solar charging rock lamp

The lamp housing appears to made of some sort of ABS moulded plastic.  The Reflector and LEDS are protected by a simple circular polycarbonate shield.  There is a battery housing and a button on the base of the housing.

Don't turn it on - Take it apart!!!

The deconstructed lamp

The light actually came apart very easily...it was mainly hot glued together!  The reflector, battery box and solar panel will be discarded as they won't have any purpose in the upcycled lamp.  I will probably leave the solar panel on as getting it off will be difficult and it won't do any harm.

There is ample space inside the lamp for a couple of circuit boards and some ballast (weight) to stop the lamp moving too easily.  My current thinking is to design two circuit boards.  One for the DMX and one for the lighting.  The controller will be a small microcontroller board which accepts DMX and outputs SPI to the lighting board.  The lighting board will be a circular PCB with WS2815 LEDS arranged in a sensible pattern.  If I'm luck it will be possible to fit 32 LED pixels on the display board.

I have not decided which microcontroller to use yet...probably an arduino or teensy variant.  There is no need to go for a wifi enabled micro as the plan is to use wire to carry both power and the DMX signal.

The diameter of the reflector is 84 mm.  I think the lamp PCB will need to be the same dimensions.  Hopefully we can get 32 W2815 LEDS (Pixels) to fit!

So to recap our electronic and mechanical requirements:

Design a lamp PCB with 32 pixels.  I think powering the lights via 12 volts might be a good idea however I will consider this more once I get to the PCB layout.  We will need to ensure the tracks are suitably rated for the current flow.  We will fuse the voltage signal on the control board with a user changeable fuse.

Nice to haves: 32 pixels - allows for simple channel assignment via DMX controllers.  Each lamp on one universe...with 96 channels.  

So the design tasks so far:
  • Design a Lamp PCB
  • Design a DMX to neopixel PCB with optocoupled DMX in and out ports - possibly using the one I've already designed.  
  • The micro is yet to be decided.
  • The circuit will also be powered via 12 V dc but we will probably need to regulate that down to 5 V dc for the micro and other circuitry...sound detection, light detection etc.
I haven't got a budget set however cheaper is always better!

That will do for now!  Take care - Langster!


Tuesday 15 March 2022

Designing a simple breakout PCB for a 3D printing project

 I was approached by a bloke on facebook for a price to design a simple breakout PCB for a 3D printing project he is working on.  As the board was relatively simple and it was for an open source project I decided to do it for him for free.  In return I get to blog about it!

The board itself was part of a 3D printer dual extruder purge controller.  The purge controller uses servo motors to control when a filament change occurs removing the requirement for a purge tower and less filament wastage.  The unused filament is extruded as blobs into a small hopper on the side of the 3D printer.

I must be clear that I did not design any of the circuitry or write any firmware so I cannot assist with the specifics to the project - all I did was layout a PCB.

The circuitry and components used were supplied as a fritzing diagram:

Blobster Fritzing Diagram


The board had to be a specific size to fit an already designed enclosure.  The board size had to be less than 47 mm by 40 mm.  After discussion with the lad on facebook the only parts needed on the board were:

1x Arduino nano every 
1x 15k Resistor
1x 10k Resistor
4x 3 way 2.54' pitch header pins
1x 4 way 2.54' pitch header pins
1x 6 way 2.54' pitch header pins
1x DC barrel jack socket

Everything else will be connected off board via the header pins.

Once all was agreed I fired up kicad and drew up the schematic diagram:

Blobster Schematic Diagram

I then selected all of the PCB footprints.  I actually find it quite useful that Kicad forces you to choose footprints before you start layout.  It prevents you from selecting parts you cannot obtain or solder!

Next I swapped to the PCB design and usually for me I chose to set the board dimensions first and then began arranging the components to suit.  Normally I rotate and move the components around in the rats nest until I get the best electrical connections and groupings and then set the PCB size.

After that was complete I did the routing of the tracks.  There was nothing special about the circuit - no high current traces or impedance matching was needed.  I didn't even bother with a ground plane - I was in a rush!

Here is the final layout:

Blobster PB layout with dimensions

  I also checked the 3D renders out just to make sure all was well.  I really find this feature useful

Top down render

Isometric view of the PCB in 3D

I then plotted the gerber and drill files and sent them to the customer (bloke from Facebook) - He uploaded them to PCBWay and got a quote for £18 including delivery!  I believe all has gone well and he is expecting delivery soon.  I will post images of the boards etc when I get them.

That's all for now - Langster!

Saturday 21 August 2021

DMX to control NeoPixels and an Isolated DMX Shield

I've been playing about with RS485 and DMX for some time now.  I am particularly interested in being able to control NeoPixels via DMX.  The amount of software and experience available for using DMX to control lights and LEDS in particular is vast.  YouTube is full of lighting shows and displays which range from humble but functional to truly epic and vast.  Here are a couple of videos which I thought were amazing:

Submergence - Squidsoup
  

Tom Betgeorge's Excellent 2020 Christmas Light Show

I must admit I watched all 35 minutes of that Christmas Show...It is incredible work.  Any way this is achieved is by controlling lots of LEDS in pre-programmed sequences using DMX control software and LED lights.  DMX is a lighting protocol based upon RS485.  Here is a brief primer on DMX:


Here is another which goes into more technical detail:


So basically lighting technicians control the lights in theatres, discos and concerts using the DMX protocol to control the brightness, colour and state (off/on) of a light or lighting fixture (many lights in one package).

DMX uses RS485 which is a differential serial protocol that electronic equipment has been using for communication since the standard was approved in 1998 and probably earlier than that.


I'm not going to go into too much detail on R485 or DMX for now other than to say it's very useful stuff for electronics and lighting fans to know about.

I have a confession to make: I'd like to be able to control lights...not just one or two but many.  If I had my way every light in my house would be sequenced and remote controlled.  Unfortunately not everyone I live with approves of this plan and as such most of my lights are still just regular lights....(boring!!!).

I have been asked in the past to assist with light shows and recently I have been asked to assist with the technical development of a light show using NeoPixels.

To that end I started to look at the DMX shields available for the Arduino and have a play.  I noticed that most of them have a flaw - they aren't isolated...that means that the electrical supply used to power  the lights is also connected via the return (or ground) to the controller.  In most cases this isn't an issue but it can be in very large lighting displays with lots of wiring and fixtures because of something referred to as a 'ground loop'.


Here are some of the DMX shields designed for use with the Arduino R3:



There is nothing functionally wrong with them and I bought two of the Concineptics variants to play with.  They arrived in good order and worked as described once I understood the code and the jumper settings.  

In order to actually use DMX properly with the software one needs a controller.  Something I was  aware of but not completely in understanding of at all.  I'd always used a hardware controller in the past.

So to really make use of DMX you will either need to make your own controller or buy one off Ebay or Amazon or wherever you might wish to.  I wanted to use an external computer and software to control my lights as I don't own or have access to a hardware DMX controller.

I bought a USB to RS485 dongle off Amazon which works perfectly...It didn't at first but that's because I cannot wire things up for love nor money sometimes.  I then bought a USB to DMX dongle and that worked straight out of the box without any issues whatsoever!  Don't tell anyone but I'm very disappointed in my engineering abilities sometimes...Not being able to wire up simple circuits competently is worrisome for somebody who works in electronic engineering!

Here are the two devices I bought which I heartily recommend:



Once you have a method of controlling DMX lights it's time to either buy some or make some...user's choice.  I was trying to make some so I got hold of some NeoPixel Tape and then connected everything up.

Before doing anything I tend to do some research...Planning is everything so they say....I watched several videos on YouTube and read some blogs on DMX and in particular this one stood out:

Gadget Reboot has an excellent channel and I recommend subscribing!

In it the narrator discusses exactly what I'm trying to achieve and provides all of the equipment required and some code.  The only issue I had was that it didn't work for me!  I didn't have the exact same setup and tried to rush things...never a good situation.  Their setup uses an Arduino mega and some potentiometers, an RS485 breakout module, Buttons and some wire for the connections - for the hardware controller.  For the receiver they use an Arduino uno and an RS485 breakout and some WS2812B LEDS.  I suspect armed with what I know now I could probably get this setup working... 

Here is my setup that worked for me:
 
A DMX to NeoPixel Receiver Setup!

The equipment needed:
1x Arduino R3 or suitable clone:  

1x RS485 breakout PCB:  
Suitable connection wire for making the connections between the arduino and the breakouts etc as well as some 4 core 120 Ohm signal cable for the RS485 communications.  For testing any wire would do but once something longer distance or more permanent is required get proper cable.  Belden 9842 is a pretty good option.  At a pinch some old ethernet cable would work...cut the R45 connectors off and use four of the cores...

The actual setup!


You will need to flash the following arduino code onto your Arduino R3:

// Target Hardware:  Arduino Uno

#include <DMXSerial.h>
#include "ws2812.h"                // a specific LED controller that disables interrupts to work better

#define NUM_LEDS 30                // number of RGB LEDs on strip - 3 LEDS in one W2182B9
#define DMXSTART 1                 // first DMX channel
#define DMXLENGTH (NUM_LEDS*3)     // number of DMX channels used (3*30 LEDs)

void setup () {

  DMXSerial.init(DMXProbe);        // initialize DMX bus in manual access mode
  DMXSerial.maxChannel(DMXLENGTH); // "onUpdate" will be called when all new ch data has arrived

  setupNeopixel();                 // setup the LED output hardcoded to pin 12 in ws2812.h

}


void loop() {
  // wait for an incomming DMX packet and write
  // the RGB data for 60 LEDs on the strip
  if (DMXSerial.receive()) {
    updateNeopixel(DMXSerial.getBuffer() + DMXSTART, NUM_LEDS);
  }

}

You will need to download a copy of the DMXSerial Library. It is available here:


Here is the source for ws2812.h - Cut and paste it into a text file called ws2812.h and store it in the same directory as your arduino sketch.

// neopixel.h


/*
  The Neopixel driving routines are taken from the article and sketch from bigjosh
  http://wp.josh.com/2014/05/13/ws2812-neopixels-are-not-so-finicky-once-you-get-to-know-them/
  where the interrupt cli() and sei() are included in the sendBit function.
  At the sources from his github this is not the case but it's important for the usage with DMXSerial library.
  (see https://github.com/bigjosh/SimpleNeoPixelDemo )
  These routines fit very good to the DMXSerial implementation because they switch on and off the
  Interrupt
  On DMX usual channels are used in the red then green then blue order.
  Neopixel wants colors in green then red then blue order so the 2 channels are switched.
*/

// ----- global defines from josh: -----

// These values are for the pin that connects to the Data Input pin on the LED strip. They correspond to...

#define PIXEL_PORT  PORTB  // Port of the pin the pixels are connected to
#define PIXEL_DDR   DDRB   // Port of the pin the pixels are connected to
#define PIXEL_BIT   4      // Bit of the pin the pixels are connected to

// This re3sults in the following Arduino Pins:
// Arduino Yun:     Digital Pin 8
// DueMilinove/UNO: Digital Pin 12
// Arduino Mega     PWM Pin 4

// You'll need to look up the port/bit combination for other boards.
// Note that you could also include the DigitalWriteFast header file to not need to to this lookup.

// These are the timing constraints taken mostly from the WS2812 datasheets
// These are chosen to be conservative and avoid problems rather than for maximum throughput

#define T1H  900    // Width of a 1 bit in ns
#define T1L  600    // Width of a 1 bit in ns

#define T0H  400    // Width of a 0 bit in ns
#define T0L  900    // Width of a 0 bit in ns

#define RES 6000    // Width of the low gap between bits to cause a frame to latch

// Here are some convience defines for using nanoseconds specs to generate actual CPU delays

#define NS_PER_SEC (1000000000L)          // Note that this has to be SIGNED since we want to be able to check for negative values of derivatives

#define CYCLES_PER_SEC (F_CPU)

#define NS_PER_CYCLE ( NS_PER_SEC / CYCLES_PER_SEC )

#define NS_TO_CYCLES(n) ( (n) / NS_PER_CYCLE )

#define DELAY_CYCLES(n) ( ((n)>0) ? __builtin_avr_delay_cycles( n ) :  __builtin_avr_delay_cycles( 0 ) )  // Make sure we never have a delay less than zero

// Low level function with mixed in assembler code.

// Actually send a bit to the string. We turn off optimizations to make sure the compile does
// not reorder things and make it so the delay happens in the wrong place.
inline void sendBit( bool bitVal )
{
  if (bitVal) {        // 0 bit
    asm volatile (
      "sbi %[port], %[bit] \n\t"        // Set the output bit
      ".rept %[onCycles] \n\t"                                // Execute NOPs to delay exactly the specified number of cycles
      "nop \n\t"
      ".endr \n\t"
      "cbi %[port], %[bit] \n\t"                              // Clear the output bit
      ".rept %[offCycles] \n\t"                               // Execute NOPs to delay exactly the specified number of cycles
      "nop \n\t"
      ".endr \n\t"
      ::
      [port]    "I" (_SFR_IO_ADDR(PIXEL_PORT)),
      [bit]   "I" (PIXEL_BIT),
      [onCycles]  "I" (NS_TO_CYCLES(T1H) - 2),    // 1-bit width less overhead  for the actual bit setting, note that this delay could be longer and everything would still work
      [offCycles]   "I" (NS_TO_CYCLES(T1L) - 2)     // Minimum interbit delay. Note that we probably don't need this at all since the loop overhead will be enough, but here for correctness
    );
  
  } else {          // 1 bit
    // **************************************************************************
    // This line is really the only tight goldilocks timing in the whole program!
    // **************************************************************************
    asm volatile (
      "sbi %[port], %[bit] \n\t"        // Set the output bit
      ".rept %[onCycles] \n\t"        // Now timing actually matters. The 0-bit must be long enough to be detected but not too long or it will be a 1-bit
      "nop \n\t"                                              // Execute NOPs to delay exactly the specified number of cycles
      ".endr \n\t"
      "cbi %[port], %[bit] \n\t"                              // Clear the output bit
      ".rept %[offCycles] \n\t"                               // Execute NOPs to delay exactly the specified number of cycles
      "nop \n\t"
      ".endr \n\t"
      ::
      [port]    "I" (_SFR_IO_ADDR(PIXEL_PORT)),
      [bit]   "I" (PIXEL_BIT),
      [onCycles]  "I" (NS_TO_CYCLES(T0H) - 2),
      [offCycles] "I" (NS_TO_CYCLES(T0L) - 2)
    );
  } // if
  
  // Note that the inter-bit gap can be as long as you want as long as it doesn't exceed the 5us reset timeout (which is A long time)
  // Here I have been generous and not tried to squeeze the gap tight but instead erred on the side of lots of extra time.
  // This has thenice side effect of avoid glitches on very long strings becuase
} // sendBit()

// Neopixel wants bit in highest-to-lowest order
// so send highest bit (bit #7 in an 8-bit byte since they start at 0)
inline void sendByte(uint8_t byte)
{
  for (uint8_t bit = 0; bit < 8; bit++) {
    sendBit(byte & 0x80);
    byte <<=
        1; // and then shift left so bit 6 moves into 7, 5 moves into 6, etc
  } // for
} // sendByte()

/*
  The following three functions are the public API:
  ledSetup() - set up the pin that is connected to the string. Call once at the begining of the program.
  sendPixel( r g , b ) - send a single pixel to the string. Call this once for each pixel in a frame.
  show() - show the recently sent pixel on the LEDs . Call once per frame.
*/

// Set the specified pin up as digital out

void sendPixel(uint8_t r, uint8_t g, uint8_t b)  {
  sendByte(g);          // Neopixel wants colors in green then red then blue order
  sendByte(r);
  sendByte(b);
} // sendPixel


// ----- defines and routines from josh - End -----

void setupNeopixel() {
  bitSet( PIXEL_DDR , PIXEL_BIT );
} // setupNeopixel()


// read data from the DMX buffer (RGB) and send it to the neopixels...
void updateNeopixel(uint8_t *ptr, uint8_t pixels) {
  uint8_t  r, g, b;

  // no interrupt is welcome.
  cli();

  for (int p = 0; p < pixels; p++ ) {
    r = *ptr++;
    g = *ptr++;
    b = *ptr++;
    // send to Neopixels
    // sendPixel(r, g , b);
    sendPixel(r >> 2, g >> 2, b >> 2);
  } // for

  // interrupt may come.
  sei();

  // Just wait long enough without sending any bots to cause the pixels to latch and display the last sent frame
  _delay_us((RES / 1000UL) + 1);
} // updateNeopixel()

// End

Once you have everything setup you will need to download some DMX software.  There is a great deal to choose from.  Some of it is very complicated and expensive and some are open source, still complicated but free for fair use.

I chose to use Q Light Controller Plus.  I have heard good things about Xlight and JinX.  

You can down load Q Light Controller Plus from here:


Once installed on the operating system of your choice, load up the software:

You will be presented with the following screen:


Click on the Inputs/Outputs button in the bottom middle of the screen.


Ensure that DMX USB is selected and that it relates to the USB Dongle.  It was already selected for me.

Next it is time to add a fixture.  Click on the fixtures icon in the bottom left corner.  You will be presented with the following screen:


Click on the green '+' Icon in the top left corner to add a DMX controlled lighting fixture.


I called mine 'LED_strip' but yours can be anything you wish.  I chose 90 channels as that is what was set in the code and I have 30 NeoPixels on my strip.  Each colour = one channel.  I noticed that Q Light Plus can only control 99 channels in one universe...Start from address 1 as there is only one fixture and therefore quantity = 1 and address gap = 0.

Anyway...Click OK and then Click on the Simple Desk button in the bottom middle of the screen.  The following window will be presented:


Now we are getting somewhere!  If you look at the display it looks quite like an audio mixing desk and in a way it is...just for lights...If you increase the slider on channel 1 you should see your first LED light up and it will be RED.  If you were to increase channel 2 It would light up up the GREEN LED in the first NeoPixel.  Channel 3 = BLUE LED etc.  The big red slider at the left hand side of the screen controls the master brightness.

Here is what I did to quickly test things:


It gives the following responses on the LED strip:


 That is about as much as I currently know on how to control Q Light Plus!  There are a million tutorials available however and a very active forum.  Go check them out!

I also had a quick play with JinX which can be downloaded from here:


I'm not going to go into how it works as the manual is OK but here is a quick video showing it in action:


That's all for now.  The next post will be about how I designed an isolated DMX shield and go into how the code works and probably a bit more on how to use DMX software.  I need to research more into the software and setting up scenes etc.

Take care everyone - Langster!

Sunday 13 December 2020

How to design a simple LED Circuit

I am out of practice at many things it would seem at the moment...

I recently had to verify a colleague's design and honestly failed miserably.  Things that I consider basic in terms of electronics I didn't see and couldn't quickly do.  
To that end let us start from a simple area and attempt to cover as many aspects as possible...at least that way I will hopefully improve in my skills and prevent others from making similar mistakes.

I'm going to show a simple circuit that I was presented with...I'll be honest I struggle to visualise circuit operation from a schematic...it's probably why I struggle with the task of assessing designs. 

Simple LED Circuit

Lets try to analyze the circuit and explain how it works.  The circuit's function is to indicate which position the switch SW1 is in to the operator.  We have two different light emitting diodes (LEDS) connected via (SW1) a double pole single throw switch to a 5 Vdc supply with two 4k7 Ohm resistors and a 10 k Ohm Resistor.

When the switch (SW1) is in the position shown above LED D2 is supposed to be illuminated and a signal (at 5 Vdc) is also passed back to a microcontroller GPIO configured as an input (not shown here) via the node labelled SW1.  When the switch is in the alternative position the LED D1 is supposed to be illuminated and at the same time a signal or rather lack of signal (0 Vdc) is passed back to the microcontroller GPIO configured as an input. 

This method of connecting LEDS is known as reverse parallel - I'd actually not heard the term before...I had seen it before but not had it described that way...

So why is this circuit not particularly well designed?  There are a couple of reasons...which I'll be honest I did not pick up on during my assessment.  I made assumptions that the designer had performed calculations and checks to ensure the values selected were correct. 

The first reason is with respect to current.  In order to cause an LED to go into illumination normally at least 16 mA of current is required. How much current is flowing in the circuit with respect to D2?

Well the forward volt drop of the LED D2 is not stated so we don't actually know...lets fix that by supplying the datasheet:

C503B-BCS-CV0Z0461 Blue Light emitting diode datasheet

D2 is a 5 mm through hole LED with a wavelength of 470 nm.  It has a typical forward voltage (VF) of 3.2 Vdc when the forward current is at 20 mA.

We can now perform a simple calculation:

(Supply voltage - Diode forward voltage) / Resistor (R2) = Current flowing in LED D2 part of the circuit

(5 Volts - 3.2 Volts) / 4700 Ohms = 0.00038297872 A or 383 µA (micro-Amperes)

In order to get an LED to illuminate at normal brightness there should be milli-Amperes - I normally aim for 16 mA.  So in the circuit shown above with the switch in this position the LED D2 will not be illuminating brightly...I suspect it won't even be visible to the human eye.

The same issue is present with LED D1, here is the datasheet:

L-53SYD Yellow Light Emitting Diode Datasheet

D1 is a 5 mm through hole LED with a wavelength of  590 nm.  It has a typical forward voltage (VF)  of 2 Vdc when the forward current is at 20 mA.

(Supply voltage - Diode forward voltage) / Resistor (R1) = Current flowing in LED D1 part of the circuit

(5 Volts - 2 Volts) / 4700 Ohms = 0.00063829787 A or 638 µA (micro-Amperes) 

If again the aim was to illuminate the LED D1 (16 mA) then when the switch (SW1) is in the alternative positon the LED D1 will not be illuminated brightly at all...

So how do we resolve this issue?  We could try changing the resistor values (R1 and R2) for a lower value...

The standard formula for calculating the current limiting resistor for an LED is:

Current Limiting Resistor (Rlimit) (Ohms) = [Supply Voltage (VS) -  Diode Forward Voltage (VF)] /                                                                                                Diode Forward Current (IF)
Supply Voltage (VS) = 5 Vdc
Diode Forward Voltage (VF) = 3.2 Vdc (Blue LED)
Diode Forward Current (IF) = 16 * 10^-3 Amps or 16 mA

Therefore:

Current Limiting Resistor (Rlimit) (Ohms) in this case R2 = (5 Volts - 3.2 Volts) / 16 *10^-3 Amps

Current Limiting Resistor (Rlimit) (Ohms) = 112.5 Ohms for the Blue LED current limiting resistor.

For the current limiting resistor for D1 we have:

Current Limiting Resistor (Rlimit) (Ohms) in this case R1 = (5 Volts - 2 Volts) / 16 *10^-3 Amps

Current Limiting Resistor (Rlimit) (Ohms) = 187.5 Ohms for the Yellow LED current limiting resistor.

As 112.5 Ohms and 187.5 Ohms are not standard values for resistors one would probably use a 100 Ohm resistor and a 180 Ohm resistor.

The other calculation that should be made when designing circuits with resistors present is to ensure that the resistor power rating is suitable for the amount of power that will be present in the circuit.  Resistor life-time is reduced and unnecessary heat is generated when too much power is conducted through resistors.

The formula for calculating the power in a component is an application of Ohms Law:

Power (Watts) = Voltage (V) * Current (I) or

Power (Watts) = (Current * Current) * Resistance or

Power (Watts) = (Voltage * Voltage) / Resistance

We can apply any version of Ohms law to calculate the information required.  I have decided to use

Power (Watts) = (Current * Current) * Resistance

Power (Watts) in R1 =  (16*10^-3 * 16*10^-3) * 180 Ohms

Power (Watts) in R1 = 0.04608 Watts or 46.08 mW

Power (Watts) in R2 =  (16*10^-3 * 16*10^-3) * 100 Ohms

Power (Watts) in R2 = 0.0256 Watts or 25.6 mW

So to ensure that the power rating for the resistors is correct we should use 100 mW or quarter watt (250 mW) rated resistors.

There are still issues with the circuit as shown above however; The circuit is kind of wasteful...When the switch is in the position shown above there will always be current flowing in resistor R1 even though LED D1 is not illuminated.  With the switch in the opposite position the resistor R2 will still have current flowing through it even though LED D2 will not be illuminated.  Why have current flowing in a resistor for no purpose...It would be better to redraw the circuit in a different way but still achieve the same circuit function.

It would also (in my opinion) be better to redraw the circuit and do away with the reverse parallel LED connections...mostly because I find it hard to visualise the circuit...

Here is the circuit which was redesigned by my colleague having had some feedback (not from me):

Improved Circuit Design

The circuit's function is still to indicate which position the switch SW1 is in to the operator.  We have a 5 Vdc supply connected to a blue LED (D2) which in turn is connected to a 120 Ohm resistor (R1) and via (SW1) a double pole single throw switch, to ground completing the circuit.  With the switch SW1 in the alternative position we have the 5 Vdc supply connected to a yellow LED (D1) which is in turn connected to resistor (R2) and via switch SW1, to ground completing the circuit.

A signal is also passed back to a microcontroller GPIO configured as an input (not shown here) via the node labelled Controller SW1.  When the switch is in the first position the LED D2 is illuminated and at the same time a signal  (5 Vdc) through resistors R3 (10 k Ohms) and R58 (1 k Ohms) is passed back to the microcontroller GPIO input. 

When the switch (SW1) is in the second position LED D1 is illuminated and the signal passed back to the microcontroller is 0 Vdc as the  R3 (10 k Ohms) and R58 (1 k Ohms) are now connected to ground as well as the microcontroller GPIO input as the input impedance of a GPIO on a microcontroller is normally 100 k Ohms.  It is a standard method of reading the state of a switch position with a microcontroller.

The current limit on each of the LEDS can be calculated for completeness:

Current limit in LED D1 (Yellow LED) = (Supply voltage - Diode forward voltage) / Resistor (R2)

(5 Volts - 2 Volts) / 560 Ohms = 0.00535714286 A or 5.357 mA (milli-Amperes)

Current limit in LED D2 (Blue LED) = (Supply voltage - Diode forward voltage) / Resistor (R1)

(5 Volts - 3.2 Volts) / 120 Ohms = 0.015 A or 15 mA (milli-Amperes)

It seems a little odd to me that the value of R2 was chosen to be 560 Ohms...that seems a little high and will cause the yellow LED to be less visible when in operation...

The power rating for the resistors can also be recalculated:

Power (Watts) in R2 =  (5.357*10^-3 * 5.357*10^-3) * 560 Ohms

Power (Watts) in R2 = 0.01607057144 Watts or 16 mW (milli-Watts)

Power (Watts) in R1 =  (15*10^-3 * 15*10^-3) * 120 Ohms

Power (Watts) in R1 = 0.027 Watts or 27 mW (milli-Watts)

Therefore a 100 mW rated or 250 mW rated resistor would be ok to use in either position...

As the circuit has been redesigned there are no conditions where unnecessary current is flowing in any of the resistors and we still have the circuit function needed...When the circuit is constructed it should work perfectly.

When designing LED circuits from now on I will always try and do the following:

1. Obtain the datasheets for any and all components to be used.  For LEDS pay particular attention the electrical characteristics: Forward Voltage (VF), Forward Current (IF), maximum voltage (VMAX) and maximum power.

2. Calculate current limiting resistor needed for the appropriate brightness by reading the datasheet and obtaining a figure for the current at the appropriate brightness...it may be shown in a graph.  Use the formula:

Current Limiting Resistor (Rlimit) (Ohms) = [Supply Voltage (VS) -  Diode Forward Voltage (VF)] /                                                                                                Diode Forward Current (IF)

3. Calculate the power flowing through the current limiting resistor and select a suitably rated component.

4. Choose a suitable resistor tolerance...in this case a 5% resistor will probably be fine.

4. Get a friend or colleague to check your circuits <slight smile>.

5. It often helps to simulate circuits but only when the correct information is provided.  It is possibly better to perform the calculations on paper as it will enforce research into the requirements.

6.  Make sure the circuit meets the requirements...If the requirements aren't known then set them before attempting to perform the design...

In writing and researching this blog post I looked at the following website for inspiration:

https://www.ngineering.com/led_circuits.htm

Apologies for the long post - hope this was helpful - Langster!