Showing posts with label LabVIEW. Show all posts
Showing posts with label LabVIEW. Show all posts

Wednesday, October 8, 2014

Shift Registers

AIM

As I promised in the last posts, I'm testing the potential of shift registers. In the experiments so far, the number of devices (switches, led, etc...) we could control was limited to the number of pins of the Arduino board. Using shift registers we can control unlimited devices, using only three pins. Like usual, I'm building a graphical interface with LabView and I'm using XBee radio transmitters.


SHIFT REGISTER 74HC595

One of the Arduino pins sends a serial binary string (0 and 1). This string is recorded in the register and it's sent to the outputs of the registers. For example if the serial string from the arduino is "00101100" (8 bit), the output pins of the register will be:
  1: off
  2: off
  3: on
  4: off
  5: on
  6: on
  7: off
  8: off

Picture from www.adafruit.com

One shift register can handle 8 bits, but we can chain them together to control more devices. In this case I'm controlling 32 LEDs, so I need 4 shift registers. A chain of registers works exactly as one register. They need a data pin (where the serial string is sent to) a clock and latch. Every time the clock pin goes from low to high, the status of the data pin is recorded. When the latch pin gets low, the recorded data is sent to the 8 output pins. If you have more than one register, you can keep sending this data to the register, and after the 8th bit it keeps going to the next register. When you set the latch low, all the registers send the recorded data to the output pins. To chain shift registers they have to share clock (pin 11, blue wire) and latch (pin 12, purple wire). The output data pin of each register (pin 9) is connected to the input data pin (pin 14) of the next one. Obviously the last register is not using the output data pin. The shift register also needs connection to power (5V) and ground.


That's how I wired them up:



And this is how it looks:




LABVIEW

The idea of this control panel is similar to the other experiments, just with more items to control.


In this case each block of 8 switches (blue, yellow, red and green) is creating a byte as in the 8 switches experiment. The bytes are then converted to strings and concatenated together, separated by commas. 

In the diagram you can see the first block of switches and lights (blue). The pink lines coming from the bottom are the strings from the other three blocks (yellow, red and green).
The delay is necessary not to overflow the buffer of the Arduino serial port.

SKETCH

The Arduino IDE includes a useful command: shiftOut(). It writes a byte in sequence of 8 bits, while switching the clock 8 times. I'm using 4 shift registers, so I have to use that command 4 times before I switch LATCH to low (so the registers set their output pins). The values from the wireless serial string is read as an integer with the command parseInt(), but it's used as a byte by the function shiftOut().


int bval;
int yval;
int rval;
int gval;

const int SER =8; //Serial output to shift register
const int LATCH =9; //Shift register latch pin
const int CLK =10; //Shift register clock pin

void setup()
{
  Serial.begin(9600);
  pinMode(SER, OUTPUT);
  pinMode(LATCH, OUTPUT);
  pinMode(CLK, OUTPUT);
}

void loop()
{
  if(Serial.available() > 0)
  {
  gval = Serial.parseInt();
  rval = Serial.parseInt();
  yval = Serial.parseInt();
  bval = Serial.parseInt();

    digitalWrite(LATCH, LOW); //Latch low
    shiftOut(SER, CLK, MSBFIRST, bval);
    shiftOut(SER, CLK, MSBFIRST, yval);
    shiftOut(SER, CLK, MSBFIRST, rval);
    shiftOut(SER, CLK, MSBFIRST, gval);
    digitalWrite(LATCH, HIGH);
  }
}

CONCLUSIONS

Shift registers are very useful and easy to use, thanks to the Arduino library. We can control hundreds of LEDs or other devices using only 3 pins of the Arduino board.


REFERENCES

Exploring Arduino by Jeremy Blum (Chapter 7 - Shift Registers)


Sunday, September 28, 2014

Wireless RGB LED and speaker control

AIM

Like the previous experiment, I want to send data from my computer to the Arduino to control a device, using radio frequencies. I'm using XBee transmitters for wireless communication. My model is XBee PRO S2: it uses 2.4GHz frequency at 50mW power, able to reach 3 km on open air. The configuration I'm using is always the same: AT mode, one coordinator (connected to the Arduino) and one router (connected to my computer). The purpose of the experiment is to change the colors of an RGB LED, and the volume and tune of a speaker, using a LabView graphical interface. This is different from the previous project because I need to send analog data now, so one byte is not enough. This time I need to send a string of values. The Arduino IDE includes a very useful command: Serial.parseInt() extract an integer number from a serial string.


ANALOG VOLTAGE

The arduino has some pins that can be used to output an analog voltage with 1023 increments from 0 to 5 V. This is not completely true: the 5 V pins are switched on and off at very high frequency so the average voltage changes. This works well in case of an LED because the human eyes can't see the LED going on and off hundred thousand times a second. What we see is the intensity of the light changing as a result of the time on and off. This tecnique is called PWM (pulse width modulation) and it works well also with motors. But in case of a speaker it's different. A speaker has to vibrate, which means that the power has to go an and off to produce sounds. The Arduino IDE has a function for that: tune() switches a pin on and off at a certain frequency to produce the sound we want in the speaker, always using the 5 V. 
As you can see in the following picture, the duty cycle (on/off time ratio) changes and the result average voltage (dotted line) changes as well. But the magnitude of the pulses doesn't change.
Picture from www.bristolwatch.com

This means that we can't use the PWM to control the volume. The only way to control the volume is to control the voltage of the pulses, using a potentiometer. But in this case I want to control everything from my LabView interface and that's why I'm using a Digital potentiometer (DigiPot). 

DIGIPOTS

DigiPots work as manual potentiometers, but they are IC (Integrated Circuits) controlled with digital signals. Some of them have an SPI communication interface like the MCP4231. The one I'm using is X9C103. It's controlled by 3 main pins: increment, up/down, chip select. The terminals of the variable resistance are High, Low, and Wiper, like in a normal potentiometer.

Picture from www.conrad.com

The digipot X9C103 has 99 increments of 101 Î© each for a maximum resistance of 10 kΩ. Plus the wiper has an internal resistance of 40 Î©. If we use the full scale in series with a speaker we find out that over the 1000 Î© it's really difficult to hear anything. For this reason I'm using only the first 10 increments, up to 1 kΩ. To do this, I made a scale from 0 to 10 in the labview interface and I add 90 in the Arduino sketch, so the resultant variable "vval" is between 90 and 100. I should have bought the digipot X9C102, which has a maximum resistance of 1 kΩ, so I would be able to use all 99 increments (of 10.1 Î© each).

WIRING

I spent many hours with this experiment rewiring the Digipot in different configuration but I only got it to work in this way: the Chip Select, Increment and Up/Down pins are controlled by the Arduino's pins 10, 11 and 12. The High terminal of the DigiPot is connected to pin 2 (the one we are going to "tune"), the Low is connected to ground, and the Wiper is connected to the positive of the speaker. The negative of the speaker is connected to ground. The DigiPot also needs a connection to 5V and ground.
The RGB LED is actually composed by 3 LEDs (red, green and blue) with common cathode (the negative terminal). Each LED needs a resistance: I'm using 220 Î© for the red and 330 Î© for the green and blue, because they have a different voltage drop. Pins 5,6 and 7 control the power to these LEDs (using PWM).
As usual, I connect an LCD screen to show me what's going on in the brain of the Arduino.


LABVIEW INTERFACE

This time it took me few minutes to create this simple page: the configuration of the VISA function for the serial communication is the same as in the previous project. Three colored bars control the intensity of the LEDs (0 to 255), and two horizontal bars control tune and volume of the speaker.


As you can see from the programming diagram, the numbers are converted to strings and then added together in an array, divided by commas. A delay is necessary not to overflow the buffer of the Arduino serial port.

SKETCH

I have rewritten this sketch many times before I got it to work.
After the serial string is read, the single values are extracted by the function parseInt(). The LED pins are set at these values straight away using PWM.
The X9C103 has its own library <DigiPotX9Cxxx.h> (download here) that allows us to use it easily. The variable "vval" (the wanted volume) is compared with the current volume and if it's different, the function pot.increase() and pot.decrease() adjust the value of the DigiPot.
The function "tune" has 2 arguments: the pin we want to tune and the value. This value can be seen in the file "pitches.h" included in the Arduino IDE. I didn't include this library because I'm not interested in playing particular notes, but I just wanna make some noise. Those values are between about 30 to 5000. I've chosen a range between 100 and 4000 to avoid acute or heavy noises.

#include <DigiPotX9Cxxx.h>
DigiPot pot(11,10,12);
#include <LiquidCrystal.h>
LiquidCrystal lcd(26, 27, 28, 29, 30, 31);

const int RED =7;
const int GREEN =6;
const int BLUE =5;

int rval = 0;
int gval = 0;
int bval = 0;
int tval = 0;
int vval = 0;
int currentvolume = 0;

void setup()
{
  pot.decrease(100);
  Serial.begin(9600);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  lcd.begin(16, 2);
}
void loop()
{
  while (Serial.available() > 0)
  {
  rval = Serial.parseInt();
  gval = Serial.parseInt();
  bval = Serial.parseInt();
  tval = Serial.parseInt();
  vval = Serial.parseInt()+90;

  if(currentvolume < vval)
    {
    int diff = vval - currentvolume;
    pot.increase(diff);
    }
   else if(currentvolume > vval)
    {
    int diff = currentvolume - vval;
    pot.decrease(diff);
    } 
  currentvolume = vval;

  analogWrite(RED, rval);
  analogWrite(GREEN, gval);
  analogWrite(BLUE, bval);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(rval);
  lcd.setCursor(5,0);
  lcd.print(gval);
  lcd.setCursor(10,0);
  lcd.print(bval);
  lcd.setCursor(0,1);
  lcd.print(tval);
  lcd.setCursor(5,1);
  lcd.print(vval);
  lcd.setCursor(10,1);
  lcd.print(currentvolume);

  tone(2, map(tval,0,100,100,4000));
}
}

CONCLUSIONS

It was difficult to wire and program the DigiPot to control the volume of the speaker because it was altering the frequencies of the "tuned" pin and hence the sound was broken. After many hours of trouble rewiring and rewriting it finally works as expected.

Tuesday, August 26, 2014

Wireless switches control (with LabView)


This is another idea I had at work: in the entertainment industry we often install distribution board and we have to walk a long way to switch something on and off. Sometimes concerts and festivals cover a large area and our distribution board can be anywhere. It would be useful to operate them wireless. It would be even better to get some data from them, for example how much current the loads are drawing on each phase, but I'll get there when I'm more expert. In this experiment I just want to control 8 switches with my Arduino, using a LabView interface and XBee radio transmitters. In the distribution board there will be relays to open and close 240V circuits using a 5V signal. In this example I'm switching on and off simple LEDs. When I wire up the distro, I just have to replace the LEDs with relays.
In this case I'm using an Arduino Mega ADK, to make sure I have enough pins to control the switches and an LCD screen.
I've chosen to control 8 switches because the serial communication between LabView and the Arduino uses 256 characters (1 byte), exactly the number of combination we can create with 8 switches (2^8). This way, we only have to send one character (= 1 byte) and we don't need to synchronize the devices or use delays. If we want to control more switches, or we want to send an analog value over 255, we would need to send a string. We will see this in the next experiment.
The wiring is as follow:

LABVIEW INTERFACE


For the LabView interface I've used the code from the example "Advanced Serial Write and Read" downloadable from the National Instrument website. I added 8 switches. They output a boolean value (0 or 1) which is multiplied by the power of 2 correspondent to that switch number:
Switch 1 output multiply by 2^0.
Switch 2 output multiply by 2^1.
Switch 3 output multiply by 2^2.
Switch 4 output multiply by 2^3.
Switch 5 output multiply by 2^4.
Switch 6 output multiply by 2^5.
Switch 7 output multiply by 2^6.
Switch 8 output multiply by 2^7.
The values are then added to form the byte we have to send to the arduino.
This number is between 0 and 255, and each number correspond to a switches configuration. for example: 84 means (1-off, 2-off, 3-on, 4-off, 5-on, 6-off, 7-on, 8-off). This byte is then sent to the serial port through the function VISA.



ARDUINO SKETCH

Now we program the arduino to read the value from the serial port and execute the commands:

#include <LiquidCrystal.h>
LiquidCrystal lcd(26, 27, 28, 29, 30, 31);
int newvalue;
int oldvalue = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}
void loop()
{
 if(Serial.available() > 0) {
   newvalue = Serial.read();
   
   if (newvalue != oldvalue){
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print(newvalue);
   oldvalue = newvalue;
   }
    digitalWrite(2, HIGH && (oldvalue & B10000000));
    digitalWrite(3, HIGH && (oldvalue & B01000000));
    digitalWrite(4, HIGH && (oldvalue & B00100000));
    digitalWrite(5, HIGH && (oldvalue & B00010000));
    digitalWrite(6, HIGH && (oldvalue & B00001000));
    digitalWrite(7, HIGH && (oldvalue & B00000100));
    digitalWrite(8, HIGH && (oldvalue & B00000010));
    digitalWrite(9, HIGH && (oldvalue & B00000001));
   }
 delay(5);
}

As usual, I attached an LCD screen to display the value of the byte received by the Arduino through the serial port. It has to be the same displayed on the LabView interface.

The XBees simply replace the USB cable. I just plug them in as I would plug in a USB cable, nothing changes at the end devices. I just have to remember the switch on the Arduino XBee Shield: it has to be on "USB" when I program the controller, and on "micro" when I use the XBee.


CONCLUSIONS



The device works as expected, but it has some limitations: the communication of one byte can only control 8 switches. In the next experiments I will try to send and read strings. Also, the number of switches shouldn't be limited to the number of pins, in the future I will use shift registers to control several switches using one pin.

Tuesday, February 11, 2014

Wireless (XBee) and serial communication, data acquisition and Labview


With this experiment I want to monitor some analog sensors and display the values on a graphical interface on my PC. There are many stages involved and there is much to learn in all of them. 

Arduino

First of all I need to wire up and program the Arduino to read the sensors. In this case I'm using six potentiometers to simulate alanog sensors. I want to display these values on a small LCD attached to the board, so I will compare them with the values displayed on the PC. To do that I need to import the LCD library at the beginning of the sketch. 
The string sent from the Arduino to the PC must contain a fixed number of digits for every variable. As I explained in a previous post, the values from the sensors vary from 0 to 1023, which means they can use one to four digits. I programmed the Arduino to divide all of these values by 1023, multiply by 900 and add 100. The result is a number between 100 and 999. So the string is always 21 digits long and each 3 digits represent a value. The opposite operation has to be done by the software on the PC to display the original values. Then I send these values to the PC using the serial connection.


This is the sketch I wrote

#include <LiquidCrystal.h>

LiquidCrystal lcd(10, 8, 7, 6, 5, 4);

void setup() 
{
  Serial.begin(9600);
  lcd.begin(16,2);
}

void loop() {
    String dataString = "";

  for (int analogPin = 0; analogPin < 6; analogPin++) {
    float sensor = analogRead(analogPin);
   int value = sensor / 1024 * 900 + 100;
   dataString += String(value);
   }
  Serial.println(dataString);

  lcd.println(dataString);
  delay(15);
}

Wireless communication

I'm using XBee radio transmitters to send data using radio frequencies. To connect the Arduino to the XBee module you need an Arduino Wireless Shield, and to connect the other XBee module to the PC you need a XBee Explorer board (see picture below). Each module has to be configured using the Explorer board and X-CTU, a free software distributed by Digi International. To communicate, the two modules have to use the same ID (the name of the network), and same baud speed; one have to be a coordinator and the other can be router, end device or both. In this case I set the one attached to the PC as coordinator and the one attached to the Arduino as end device. They can be configured to create very complex mesh networks, but I don't need it now.
I don't need to modify the sketch on the Arduino because the serial data flows through the XBee module. The wireless shield has a switch: it must be on “USB” when using serial to upload a sketch on the microcontroller, and on “MICRO” when using the XBee to send data.
The XBee module I've chosen are: XBee PRO series 2. They use 50 mW power at 2.4 GHz frequency (legal in Australia). The range of this model is about 3 km outdoor. 
I bought all the components from http://www.australianrobotics.com.au/, and I think they have good items, reasonable prices and an excellent service (I talked to them because I thought the explorer board didn't work). Here are all the parts I bought, but you can chose different combinations:

X-CTU has a "terminal" window where we can see the data received on the PC, but only one software at a time have access to the serial connection (Arduino IDE, X-CTU or LabVIEW).


The following pictures show the data string as transmitted to serial (read by the Arduino monitor), and as transmitted wireless (read by the X-CTU terminal). The left one shows broken data because the Arduino was working at maximum speed. So I had to add a delay of at least 15 milliseconds in the sketch and the data was received properly as in the right picture.



LabVIEW

I was able to get a student copy of LabVIEW so I’ll try it. It’s used in SCADA (Supervisory Control And Data Acquisition), for example to monitor and control oil rigs in the middle of the ocean, from an office in the city centre. It’s also used in military and space applications. The programming language of LabVIEW is graphical; it’s like a flow diagram, but not that easy. Sparkfun sells a kit with an Arduino UNO board and an old version of LabVIEW (here). That’s not the way I got it but I think it’s a good deal. Getting started with LabVIEW is easy: you have to use two windows, one to create objects graphically, and the other to connect them together and make them work.
To learn the programming language of LabVIEW I found these tutorials very useful.
LabVIEW needs another software to be able to read serial data. It's called VISA Shared Components and it's not included in the basic software. I had it installed for a coincidence (it was together with my dive computer software).
I noticed that LabVIEW has a library to program the Arduino boards, so I installed it and I had a look. You can use it to control the Arduino, but the program is not deployed on the board. So, to work, the Arduino has to be connected to the PC and LabVIEW, which is not what I want now.
I created a basic interface where the values are displayed graphically.



To read the serial data you need the VISA function. I tried some examples from the National Instruments tutorials, but they were all slow, the data was updated once a second. So I tried this example and the data rate was very quick. The configuration of the serial port is as follow:


The following picture shows the main loop:


The pink line is the string read by the VISA function. This string is then divided in substrings and converted to numbers. The operator can set a maximum value, when the value from the sensor goes over that limit, an alarm lights up. The white icon is a group of functions I've put together in one to make it easier to repeat it for every value. This group of functions is shown in the following picture:


It converts the string to number, calculate the original value and the max value.


Conclusions

I’ve set up my Arduino Uno to send data through serial port. The Arduino reads analog inputs from potentiometers (they simulate real sensors, sending an analog voltage) and keeps sending a string with all the data together through the serial port. The XBees modules send this data wireless to the PC. Labview reads this string (VISA function) and takes every single data out of the string. The single data is then displayed graphically.