Wednesday, February 12, 2014

Low-pass filter and integrator in Excel


Today I want to publish a couple of useful spreadsheet I made to solve electrical circuits.
The first is an integrator circuit: a pulsing DC voltage charges a capacitor in steps. This spreadsheet calculates the voltages across the capacitor over time. It's a pretty complex formula to do it manually.

Download the file

The second spreadsheet calculates the frequency response in a series RLC circuit, in this case the voltage is taken across the capacitor, therefore it's a low-pass filter.

Download the file

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.





Friday, February 7, 2014

Solar Mobile Workshop

This is one of the earliest project I have done in TAFE during my Renewable Energy course. 
The aim was to design a solar system for a caravan. I wanted to do something different and innovative, so I designed a caravan which can be used as a workshop when working in a remote location. For example it could support the construction of a shack in the bush, and provide a basic accomodation for two workers in the same time. The calculations in this project are done with the "rule of thumb", instead of the formulas provided by the Australian Standards. Reviewing this project now, I could do some improvements: the inverter should be larger, and I would use an integrated inverter/battery charger like the Sunny Island from SMA. A backup generator could provide some extra power to operate a welder or other power demanding tools. This would increase the weight and will require a very strong structure for a caravan.
This design need to be improved, but the idea is still innovative and useful.

Click here for the specification
Click here for the tender
Click here for the full report

I have done the drawings with Google Sketchup, a very easy and free 3D design software. To draw the caravan I used some parts downloaded from Google Warehouse.








Datalogger

Here I start with a simple project with my Arduino: a data logger. I want to monitor temperature, humidity and light and record all the data on SD card. To do that I need my Arduino Board, a photo or light sensitive resistor (it changes its resistance according to the light), a temperature and humidity sensor and an SD module (and of course breadboard and jumper wires).
The Arduino software (or IDE) is free and comes with many examples. You can also find many useful information on their website and the forum. You can buy Arduino boards and other components from Ebay and many electronics shops. Genuine boards and clones work exactly the same, but only buying genuine boards you support the developers who also provide free software. Genuine boards are marked "made in Italy" (not "design in Italy"). A reasonable price in Australia for a genuine Arduino UNO is about 32 $. I bought a lot of electronic components and sensors from CoreElectronics and I was happy with them, they have genuine boards and also sell on Ebay. If you are not in a hurry, you can buy the other components and sensors from China and Hong Kong on Ebay and save some money.
To learn how to program and wire them up, I found this series of tutorials very useful. If you need to start from the basic then this series of tutorials would be easier.
The light resistor is wired as a voltage divider in series with a fixed resistor. The Arduino reads the voltage across the photo resistor through the Analog input 0. This value will be between 0 and 5 volts (depending on the value of the fixed resistor, I’m using a 1.4 kΩ) but the value read by the Arduino will be between 0 and 1023, because the Analog to Digital Converter uses 10 bits (2^10 = 1024 values).
The temperature and humidity sensor is a cheap DHT11 sensor. It has an internal circuit able to read the analog voltages, calculate the right values and send a string with these values to the Arduino. The sketch or program in the Arduino must include the DHT11 library to be able to read this string.
The SD module is able to read and write data on a file .log or .txt, but only one file at a time. The communication used by the DHT11 is SPI (Serial Peripheral Interface), it needs 4 pins plus ground and VCC (5 volts). These 4 pins in the Arduino UNO are 10 (SDCS), 11 (MOSI), 12 (MISO) and 13 (SCK). The sketch must include the SD library in order to use the commands regarding the SD module.
To do the schematics I used Fritzing, an useful, easy and free software (you can draw PCB as well).


And this is how it looks in real life:

To write the sketch I’ve copied some parts of the Datalogger example sketch and DHT11 sketch:

#include <DHT11.h>
#include <SD.h>

int pin=3;
DHT11 dht11(pin); 

const int chipSelect = 4;

void setup()
{
  pinMode(10, OUTPUT);
  
  if (!SD.begin(chipSelect)) {
    return;
  }
}

void loop()
{
  int err;
  float temp, humi;
  if((err=dht11.read(humi, temp))==0)
  {
  int t,h;
  t = (int) temp;
  h = (int) humi;
  String dataString = "";
    dataString += "temperature:";
    dataString += String(t);
    dataString += ",";
    dataString += "humidity:";
    dataString += String(h);
    dataString += ",";
    dataString += "light:";
    dataString += String(analogRead(0));
        
  File dataFile = SD.open("datalog.log", FILE_WRITE);
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
  }
  }
  delay(30000);
}

I’ve tried many times with different SD cards and looks like some SD cards cannot be read by the SD module, even if they work fine in cameras and other devices. They have to be formatted as FAT (16 or 32), but even then, not all are readable. Anyway, I found one that works and I started recording.
This is how the data was recorded in the file, every 30 seconds (the board doesn't know what time it is, so you should mark the time when you start recording):

temperature:27,humidity:36,light:658
temperature:27,humidity:37,light:671
temperature:27,humidity:36,light:678
temperature:26,humidity:37,light:673
temperature:26,humidity:37,light:670

After few hours I plugged the SD card into my computer, imported the data in Excel and plotted a chart.