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.
No comments:
Post a Comment