if ( !advertising ) {

      useCodeLines(LAME_JOKE);

   }

Route / Route / Route

dynamic_feed comment

3Dip

Fourier LEDs with LabVIEW


Fourier LEDs with labVIEW

calendar_today 20/05/2020  |  today   21   |  visibility   14656



[Status]: Building How-to.

In this project:
1. We're gonna use labVIEW 2019
2. We'll send serial data from PC to Arduino
3. We're gonna use the PC microphone (external mic works as well)
4. We'll be using a bit of signal processing


Introduction. labVIEW and serial communication

"What is labVIEW?" from National Instrumetns - About page

LabVIEW is systems engineering software for applications that require test, measurement, and control with rapid access to hardware and data insights.

LabVIEW offers a graphical programming approach that helps you visualize every aspect of your application, including hardware configuration, measurement data, and debugging. This visualization makes it simple to integrate measurement hardware from any vendor, represent complex logic on the diagram, develop data analysis algorithms, and design custom engineering user interfaces.

how to make LED flashing - NI Community - National Instruments

 

 

"Serial Port Communication" from National Instruments - Article

Serial communication transmits data between a computer and a peripheral device, such as a programmable instrument or another computer. Serial communication uses a transmitter to send data one bit at a time over a single communication line to a receiver. Use this method when data transfer rates are low or you must transfer data over long distances. Most computers have one or more serial ports, so you do not need any extra hardware other than a cable to connect the instrument to the computer or to connect two computers to each other.

You must specify four parameters for serial communication: the baud rate of the transmission, the number of data bits that encode a character, the sense of the optional parity bit, and the number of stop bits. A character frame packages each transmitted character as a single start bit followed by the data bits.

Baud rate is a measure of how fast data moves between instruments that use serial communication.


Now, There are many firmware already built (such as LIFA) to help you send and receive data from the serial bus, that is, the usb cable from your PC to the power/data Ardiono input. But that results in a large and muilti purpose code and what im trying to do is build a code from scratch that runs as fast as possible to only receive serial data from labVIEW (PC) to Arduino.

Even though building a entire firmware sounds like a laborious task, it's quite simple in labVIEW and in Adruino as well. But most important, building a code oneself will help you understand what exactly your software does and apply changes if you will.

Aditionaly, this software will have full compatibility with most Arduino boards
 


The blocks we're gonna use to manage the serial connections are:



From left to right.

VISA resource name. This is the port to send the serial data. The names are COM0, COM1, ...

Configure Serial port. This block is the serial port initializer. baud rate, timeout, end character, data bits, etc.. Most configurations are alright by default but since the speed is the priority, we'll use a 115200 baud rate.

VISA clear. Clears the input and output buffers, this is needed after initializing.

VISA Flush I/O Buffer.  Clears the input and output buffers as well but somehow it doesn't work if it's not here.

VISA Write. Sends the data in the "write buffer input" as text. We´re gonna send a string like: {12,34,56,78}. Just that simple

VISA Close. ends the serial comunication. If the VISA Close is not called at the end of the execution program, the initialization might not work next time.


Comon errors

Invalid Source.
This happens when the resource name (COMX) is not the Arduino board or you didn't selected any.
In case that the dropdown list doesn't have any connections it is likely a driver causing problems.
In case you did selected a resource name and is the correct one, try restarting you labVIEW program.

Making of the Hardware

1. Using 18 wooden flat sticks and 2 square sticks, mark and cut as follows:
 
2 Outside Pillars (P1)
9 Inside Pillars (P2)
4 Lower (breadboard) Bases (B1)
1 Front Base (B2)
2 Square stick Bases (B3)
1 Front Roof (R1)
1 Upper Roof (R2)
 
 


Apply black painting so it doesn't reflect any color.



2. Assemble the parts as follows









 

3. Now the RGB LED array using a half breadboard.









4. Finally, Follow the schematic connecting the Arduino microcontroller and the Integrated Circuit ULN2803

Render of 8 RGB LEDs simultaneously working Using a ULN2803

Inside the ULN2803:

Render of How to use a ULN2803 as a multiplexor




 

Building the labVIEW program


We're gonna split the program in several blocks so it looks cleaner and understandable. The front panel looks like this:

And the complete block diagram is:



The first block contains the initializers:


The VISA block were already explained in the introduction to serial communications part.
The Soind reading part is actually a sub VI prefabricated by LabVIEW, it wasn't changed at all, it only needs the Task ID (sound input device) and the samples/ch given previously. At the output only needs to extract the waveform inside the output array using a "index array" block.
 


 
Now, remembering a bit of signal processing. The data that the microphone gives us is a signal in the time domain like this
Understanding Audio data, Fourier Transform, FFT and Spectrogram ...
And after applying a Fast Fourier Transform (FFT) it turns to:
Fast Fourier transform - MATLAB fft
What we need now is split the frequency range into 8 equal parts and add all of the components or armonics inside its own split part.

The value of 1.8 and 1.6 might reprecent a red bright color while a 0.4 and below is a dim blue color, and that's exactly the FFT block:





 
The ranging of values must change the color of the lights as well as the intensity so the Fourier LEDs looks cooler.
First, we're gonna use a scale to 0-305 to match the color algorithm, this algorithm is nothing but a numbers play, the highest number is red while a low number is blue. Finally, the last part ranges the intensity of the color, the the highest number is bright while a low number is dim, it also has a blue smoother to decrease the illumination of the 'blue' armonics (quiet armonics).


Last but not least, the string concatenation. Here we build the output string by adding a "{" at the beginning of the string and a "}" at the end (this two characters are added in the main blocks, not here). Then the Red value, the Blue value and repeats until all eight loops are finished completing a total of 16 numbers (8 red and 8 blue).
 



 

Building the code with Arduino

Here's the code used for Arduino. I used an Aruino nano but should work for UNO as well.


 

 
In general, it waits for the serial data to input, stores the data contained between the characters '{' and '}', then shows each of the eight RGB LEDs information carried by the "receivedChars" variable.

The "receivedChars" variable carries the information of the eight RGB LEDs like:
"255,0,200,0,0,100,0,50,0,10,0,2,0,0,0,0"
Where the first set of data is 255,0. This is the color chanels R and B for the first RGB LED and so on.

The base code for serial data transmission comes from the article on the Arduino forums at https://forum.arduino.cc/index.php?topic=396450 it just has one part modified for the RGB LEDS which is:
 
void showOnce(){
    char *token;
    static char prevSelection = 0;

    strcpy(input,receivedChars);
    
    token=strtok(input,",");
    digitalWrite(selectors[7],LOW);
        
    for(i=0; i<8; i++){
        for(j=0; j<2; j++){
            analogWrite(PWMOutputs[j],atoi(token));
            token=strtok(NULL,",");
        }
        digitalWrite(prevSelection,LOW);
        digitalWrite(selectors[i],HIGH);
        prevSelection = selectors[i];
        delay(1);
    }
}


What this part does is simply show one led at a time at a high speed enough to make the result look like all of the RGB LEDs are turned on their respective colors at the same time.

First, uses the "input" variable to store the first value (chanel R of first set of data) of "receivedChars" and assignis it to the PWM output. Note that the serial data is a string of characters and is then converted to integrer (atoi).

Then selects the corresponding selector[] to turn on. The delay is so the time is turned on is longer than the time is turned off, this way it looks brighter.

And repeats changing the corresponding color with the corresponding RGB LED until all eight made a cicle.

Is important to know that the function "showOnce()" actually shows once so it returns to the main function and asks if theres new data to show, if there isn't, returns to "showOnce()" again, if there is, collects the new data and "showOnce()" the new data.






@ab-cb
@dip000


Este sitio web fue creado de forma gratuita con PaginaWebGratis.es. ¿Quieres también tu sitio web propio?
Registrarse gratis