Sei sulla pagina 1di 11

electronicsblog.

net
Just another electronics blog

Digital voltmeter – Arduino and PC (Visual C++)


comunication via serial port

Today I will show how to make digital bridge between Arduino and PC: control analog – digital converter and
send measured data to PC. Windows application will be created using Visual C++ 2008 Express.

Voltmeter demo software is very simple, and here is a lot room for improvement, but I just wanted to show
basics how to control com port and execute data exchange between PC and Arduino.
Communication between Arduino and PC:

ADC measurement loop starts when PC sends 0xAC and 0x1y  to Arduino. Where y is ADC channel
number(0-2);
Measurement stops after Arduino receives 0xAC and 0x00;
In measurement loop with 50ms delay Arduino sends 0xAB 0xaa 0xbb, where  aa and bb is higher and
lower bytes of measurement result.

Arduino code

More about serial communication You could nd at arduino.cc. Code is simple, the biggest part of it takes
switch structure that takes action if data available at serial port bu er. If it’s so ADC conversion is started
on selected channel, or stopped accordingly to received data.

After ADC conversion is nished there is 10 bit voltage value (0x0000-0x0400) in 16 bit variable (int). Serial
(RS-232) protocol enables sending data in packets, where raw data can contain maximum 8 bits. It gives a
need to split 16 bit variable in 2 parts of 8 bits. To get higher byte variable is shifted to right by 8 bits. To get
lower byte variable is divided by 256 and remainder is taken.

    Serial.print(voltage>>8,BYTE);
    Serial.print(voltage%256,BYTE);

Full Arduino code:

//electronicsblog.net 
//Arduino communication with PC via serial port.

int voltage=0;
int channel =0;
unsigned char incomingByte = 0;
boolean measure=false;

void setup() {

  Serial.begin(9600);
}

void loop() {

  if (measure) {

    voltage=analogRead(channel);
    Serial.print(0xAB,BYTE);
    Serial.print(voltage>>8,BYTE);
    Serial.print(voltage%256,BYTE);
    delay(50);

  }

  if (Serial.available() > 0) {


    delay(10);

    if(Serial.read()==0xAC) {
      incomingByte =Serial.read();

      switch (incomingByte) {

      case 0x10:
        measure=true;
        channel=0;
        break;

      case 0x11:
        measure=true;
        channel=1;
        break;

      case 0x12:
        measure=true;
        channel=2;
        break;

      case 0x00:
        measure=false;
        break;

      }

    }

  }                

};

Visual C++

I assume, that You already have a basic knowledge of Windows forms programing in C++,  if not when just
Google, internet is full of examples for beginners.
First thing to do is add serial port resource from toolbar to area below form. Properties allows to change
some important settings of  serial port: port name, baud rate, databits.  It’s more useful to add controls in
application window to change these settings any time, without need to recompile program again. 
“Voltmeter demo” application have only one of these control and it’s dropdown list of available ports.

After retrieval of available serial port names list the rst port is selected as default. Code for that trick:

array< String ^>^ serialPorts = nullptr;


serialPorts = serialPort1->GetPortNames();

this->comboBox1->Items->AddRange(serialPorts);

this->comboBox1->SelectedIndex=0;

Serial port in PC could be used only by one application at one time, so port must be opened before use, and
closed after no longer needed. Simple commands for that:

serialPort1->Open();

serialPort1->Close();

Reading serial port.  To do is properly, it’s necessary to  use “received data” event (interrupt) .  At serial port
properties select events.
When double click “DataReceived” drop-down list.

Event’s function code is automatically generated:

private: System::Void serialPort1_DataReceived(System::Object^ sender,

System::IO::Ports::SerialDataReceivedEventArgs^ e) {

Code from “Voltmeter demo”:

Where is a check to see if rst byte arrived to serial port is 0xAB, if is it means, that other bytes carries
voltage data. In other case blank reading is executed to clean serial port bu er.

private: System::Void serialPort1_DataReceived(System::Object^ sender,

System::IO::Ports::SerialDataReceivedEventArgs^ e) {

unsigned char data0, data1;

if (serialPort1->ReadByte()==0xAB) {

data0=serialPort1->ReadByte();

data1=serialPort1->ReadByte();

voltage=Math::Round((float(data0*256+data1)/1024*5.00),2);

data_count++;

}
serialPort1->ReadByte();

Sending data/writing serial port.

Some problem for me here was to send raw hexadecimal data via serial port. Write(); command was used,
but with 3 arguments: array, start byte number, number of bytes to write).

private: System::Void button2_Click_1(System::Object^ sender, System::EventArgs^ e) {

unsigned char channel=0;

channel=this->listBox1->SelectedIndex;

array^start ={0xAC,(0x10+channel)};

array^stop ={0xAC,0x00};

if (!adc_on) {
serialPort1->Write(start,0,2);

this->button2->Text="Stop";
adc_on=true;

} else {
serialPort1->Write(stop,0,2);
this->button2->Text="Start";
adc_on=false;}

That’s all what I wanted to share. If You need full Visual C++ code please ask it in comments, and i will
upload it.

This entry was posted in Arduino and tagged ADC, communication, RS232, serial port, visual c++ on October
4, 2010 [https://www.electronicsblog.net/digital-voltmeter-arduino-ant-pc-visual-c-comunication-via-serial-
port/] .

43 Comments Just another electronics blog 


1 Login

Sort by Best
 Recommend 5 ⤤ Share

Join the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

William Friedel
− ⚑
4 years ago
Note: for those using newer arduino compiler change;

Serial.print(0xAB,BYTE);
Serial.print(voltage>>8,BYTE);
Serial.print(voltage%256,BYTE);

---to---

Serial.write(0xAB);
Serial.write(voltage>>8);
Serial.write(voltage%256);

works perfect after that! Thanks for the tutorial!


2△ ▽ Reply
Sebi
2 months ago
− ⚑

hello, is it possible to measure up to 15V at the analog pin A0?


△ ▽ Reply

Jozef Kirňak
− ⚑
4 months ago
Hello!
Please send me the full Visual C++ code Serial Voltmeter. Thank you!
△ ▽ Reply

Kevin
− ⚑
9 months ago
Hello!
Please send me the full Visual C++ code Serial Voltmeter. Thank you!
△ ▽ Reply

Darius > Kevin


− ⚑
9 months ago
https://www.dropbox.com/s/4...
△ ▽ Reply

Kevin > Kevin


− ⚑
9 months ago
My e-mail is tonextway_k_0607@yahoo.co.jp. Thank you
△ ▽ Reply

Ted Krueger
− ⚑
a year ago
Please send me the full Visual C++ code for the Arduino Serial Voltmeter. Thank you!
△ ▽ Reply

Darius > Ted Krueger


a year ago
− ⚑

https://www.dropbox.com/s/4...
△ ▽ Reply

Dadan
3 years ago
− ⚑

please, gave me C++ code


Digital voltmeter – Arduino and PC (Visual C++) comunication via serial port
△ ▽ Reply

Laura
− ⚑
3 years ago
Do you have the code on Visual C# ??
I'd be wondering if you send me the code

l it 01@h t il
laurita_sexy01@hotmail.com
△ ▽ Reply

Laura > Laura


3 years ago
− ⚑

and could you tell me the components and the diagram please
△ ▽ Reply

Darius > Laura


3 years ago
− ⚑

Just Arduino board connected to computer via USB port. And voltage is sensed ant first analog
pins.
△ ▽ Reply

Darius > Laura


3 years ago
− ⚑

No, it is only written for C++.


△ ▽ Reply

Umer Khalid
4 years ago
− ⚑

how to link the"Label1", as mention above, which display the measurment .................. with voltage,
the variable you mention in the code, ..................... i got the error cross thread opearion is not valid.
whats that???
△ ▽ Reply

mohammad
5 years ago
− ⚑

Hi
Is it possible that you give me voltmeter circuits

Thank you.
Mohammad
△ ▽ Reply

Darius > mohammad


5 years ago
− ⚑

It's nothing to give, voltage source is connected directly to Arduino Analog pins.
△ ▽ Reply

Johnny
6 years ago
− ⚑

Good morning,
you use 2 pair of byte for read the max value in C++ of "99,99" but if i want to have a higher precision
(for example a voltage with 4 numbers after the coma "99,9999") what i need to change?

Bye, thanks for the reply ^_^


△ ▽ Reply
Darius > Johnny
6 years ago
− ⚑

Real higher precision can be achieved only is ADC have higher resolution. With 10 bit ADC there is
1024(2^10) steps, so if ADC reference(max value that can be measured) is 5 V, then measurement
resolution is ~ 0.005 V.
But for show you could have 4 numbers after coma.
Find this line in Form1.h
voltage=Math::Round((float(data0*256+data1)/1024*5.00),2);
replace to voltage=Math::Round((float(data0*256+data1)/1024*5.00),4);
voltage now would be rounded to 4 numbers after coma instead of 2.
⛺ View

1△ ▽ Reply

Atar
− ⚑
6 years ago
thank you, its helping me so much... :)
△ ▽ Reply

Enrique Mora Fuster


− ⚑
6 years ago

THIS COMMENT IS NOT RELATED WITH YOUR EXPERIMENT: I am not an expert in digital
electronics. But I made a TRIAL ERROR METHOD experiment. "Transfering Files and Configuration" 
From a PC to another PC through COM DB9 ports (SERIAL CABLE) using Windows XP SP2. At the
incredible speed of 19200 Bauds. And it worked. I guess a 7 Megabyte file can be transfered in less
than 10 minutes. I used the pin connections explained in MSDOS 6.22 Help program for INTERLNK
(serial cable). You must have Qbasic.exe installed in the same carpet where help.com is. You also
may watch the help program for MSDOS 6.22 open it on a window WITH WINDOWS XP SP2.
rEMEMBER THE msdos6.22 files must not be in C:\windows but in another different
carpet.WINDOWS XP SP2 DOES NOT PERMIT TWO DIFFERENT OPERANTING SYSTEMS ON THE
SAME PARTITION. MSDOS 6.22 is a 8 bit operating system and Windows XP is a 32 bit operating
system. Here there are the five files to read the HELP program for MSDOS
6.22.....http://www.4shared.com/zip/66N__fi... ..........(the zipped file was made with WINZIP 9.0) and
can be downloaded in three seconds. (PLEASE DON`T CRASH YOUR SYSTEM)

see more

△ ▽ Reply

Load more comments

ALSO ON JUST ANOTHER ELECTRONICS BLOG

How to connect to diagnostics port of AC Reading temperature from LM92 digital


Stag gas controllers without … sensor with Arduino
Stag gas controllers without … sensor with Arduino
16 comments • 4 years ago 2 comments • 4 years ago
Nick Jarvis — hey there, did you have any luck Darius — Yes, this is a typo. Thanks for the
Avatarusing the Arduino Micro yet? Can anyone help Avatarnotice.
with this board ? I …

Arduino based Temperature and How to edit a Mini 0805 DVR firmware and
Humidity/Humidex meter with … have a custom video …
3 comments • 4 years ago 2 comments • 2 years ago
Kévin Pagnat — Thanks you very much for Darius — Don't have direct answer. One way is
Avataryour tutorial. I made it in just an hour ! Avatarto modify FW that is working with this tool.
And then …

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Privacy

Potrebbero piacerti anche