🔌 Digital Voltmeter on Arduino with PC connection via serial port

This article shows how to connect Arduino and a PC and transfer data to the PC from the ADC. A Windows program is written using Visual C ++ 2008 Express. The program of the voltmeter is very simple and has an extensive field for improvements. Its main purpose was to show the work with the COM port and the exchange of data between the computer and the Arduino.

Communication between Arduino and PC:

  • The reading from the ADC begins when the computer sends the Arduino 0xAC and 0x1y commands. y is the ADC channel number (0-2);
  • The reading stops when the Arduino receives 0xAC and 0 × 00 commands;
  • During the reading of Arduino, once every 50 ms, the computer sends the commands 0xAB 0xaa 0xbb, where aa and bb are the maximum and minimum measurement results.

Arduino sketch:

You can read more about serial communication at arduino.cc. The program is quite simple, most of it is working with a parallel port. After the end of the removal of data from the ADC, we get a 10-bit voltage value (0 × 0000 - 0 × 0400) in the form of 16-bit variables (INT). The serial port (RS-232) allows you to transfer data in packets of 8 bits. It is necessary to divide the 16-bit variables into 2 parts of 8 bits.
Serial.print (voltage >> 8, BYTE);
Serial.print (voltage% 256, BYTE);
We shift the variable bytes by 8 bits to the right and then divide by 256 and send the result to the computer.

Full source software for Arduino you can download here.

Visual C++

I assume that you already have a basic knowledge of programming in C + + for Windows if not, use Google. The Internet is full of lessons for beginners.

 The first thing to do is to add a serial port from the toolbar to the bottom form. This will allow you to change some important parameters of the serial port: the name of the port, the data transfer rate, the bit depth. This is useful for adding controls to the application window, for changing these settings at any time, without recompiling the program. I used only the ability to select the port.

 After searching for available serial ports, the first port is selected by default. How it is done:

array< String ^>^ serialPorts = nullptr;
serialPorts = serialPort1->GetPortNames();
this->comboBox1->Items->AddRange(serialPorts);
this->comboBox1->SelectedIndex=0;


A serial port on a PC can only be used by one application at a time, so the port must be open before use and not closed. Simple commands for this:
serialPort1->Open();
serialPort1->Close();

To correctly read data from the serial port, you need to use events (in our case, an interrupt). Select event type:

Drop-down list when you double-click "DataReceived".

Event code is generated automatically:

private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
}


If the first byte arrives on the serial port 0xAB if it means that the remaining bytes carry voltage data.

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();
}

Writing and reading serial data

For me, the small problem was sending hex RAW data through the serial port. The Write() command was used; but with three arguments: an array, the starting byte number, the 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;}
}

 

 

 

 

 

 

 

 

 

 

 

Leave a comment

Name .
.
Message .

Please note, comments must be approved before they are published