IAN LANG ELECTRONICS

In the last study we made the PC send serial data to the Arduino and that data controlled what the Arduino did with an attached LED. Now we're going the other way round and letting the Arduino influence what the computer does with an interface made in Visual Basic. Perhaps the most practical learning tool is making a thermometer with the Arduino and sending the measurement to the computer to be displayed.

Controlling your PC by Arduino

Go Back

On the right is the VB form I made to use as the interface. It consists of a label and a rich text box. The AAA is in there as a test text, it could just as easily be blank but I wanted to be sure three characters could fit in there and just never got rid of it. The rich text box, wherein AAA is written, is going to display the numerical value of the temperature as measured by a TMP 36 attached to the Arduino Uno. You need to add three components, two timers and a serial port, I called my timers Timer 1 and Timer 2 and the serial port comport. All of them are found in the VB 2010 toolbox.

The port is going to remain open throughout, and this is Timer 2's job as we'll see. Timer 1 polls the serial port for incoming data. But having a GUI is no good if we don't have the physical hardware to back it up. Here's how to wire up your UNO board:

The TMP36 is a low-cost IC which looks rather like a transistor but is in fact a fully integrated measuring device which outputs 10 mV for every 1 degree centigrade it detects with an offset of 500mV for measuring temperatures down as low as -50 degrees centigrade. It is quite accurate, and though not to medical standards it is plenty good enough for ambient temperatures.

The code for the Arduino reads the output from the TMP 36 and converts it to a reading in centrigrade:

 

 

char outstring;

long centtemp;

void setup(){

  Serial.begin(300);

}

void loop(){

 

centtemp = analogRead (A0);

centtemp = map(centtemp,0, 1023, -50, 460.8);

 

outstring=centtemp;

 

Serial.write(outstring);

delay(250);

}

 

and the actual work in the interface is done by timer 1:

 

 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim incoming

        incoming = comport.ReadChar

     

 

            RichTextBox1.Text = incoming

 

 

    End Sub

 

Timer 2 merely opens the serial port and then switches itself off:

 

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

        comport.Open()

        Timer2.Enabled = False

    End Sub

 

It takes no more part in the run-time of the program. Cantering through, here's how it all works:

 

The Arduino reads the output voltage of the TMP 36 and puts it to a variable called centtemp:

 

centtemp = analogRead (A0);

 

At the minimum the digital voltage step is 0, and this corresponds to a temperature of -50 deg C. The maximum is 1023, and this is 450 deg C. But the digital steps of the Arduino go in 4.89 mV bands. This is an embuggerance, but a bit of head-scratching and pencil-chewing gives 460.8 as the top in degrees the TMP 36 and the Arduino together can measure. Hence we can map the voltage range against the temperature range thus:

 

centtemp = map(centtemp,0, 1023, -50, 460.8);

 

After this, it's a case of assigning this to an output variable. A char type is best because it can act as a string but handle numerics. At the PC end we've got to have a string to assign to the rich text box. So:

 

 

outstring=centtemp;

 

and then we chuck it out through the serial port:

 

Serial.write(outstring);

 

then we we wait a quarter second before doing it again.

 

delay(250);

}

 

At the other end we set the interval of Timer 1 to be the same 250 milliseconds, and give it this code:

 

        Dim incoming

        incoming = comport.ReadChar

     

 

            RichTextBox1.Text = incoming

 

so it reads what the Arduino has sent, assigns it to the variable incoming and then assigns the value of incoming to the rich text box.

 

And that's it. Really. A very simple protocol offers good results. It's been a theme running throughout this website that electronics is as difficult as you make it. Complicate the nuts and bolts, and you complicate the problems too. Keep it as simple as you can, and you have much less problems.

 

Over the page, lets complicate the issue a little more. Let's have it switching between C and F by the click of a button.

 

More>