Arduino & C# – HC SR04 Distance Meter C# App For Arduino

Hello world! Today i made a C# App For Arduino to display The distance with a HC-SR04 Ultrasonic sensor, The code is very simple and you could also use the app for other sensors to display a value by simply sending the value to serial Serial.println(distance); or whatever you name your value, so if you wanna display temperature you can simply display temperature on this app by sending the value from arduino to the serial.

Files you modify in the C# source are the following. Form1.cs & Form.1Designer.cs
To modify the look of the Application you simply double click on Form1.cs and the Form1.cs [Design] should pop up. You can now drag and drop buttons and whatever you want from the toolbox, and change background in the properties etc. If adding a button for example, double click the button to give it a function, Double clicking it will bring you to the part of the code that belongs to the button.

You can also change the about box by double clicking on the about box AboutBox1.cs, You can change background, font, colors etc.
To change company name, version etc. you can double click Properties in the [solution explorer]. And then at [application] click on [assembly information], you can also modify AssemblyInfo.cs directly.


HC-sr04 distance meter
HC-sr04 distance meter

Simply wire it up like the schematic and upload the arduino code. Open the windows application and press the read distance button.

Parts list.


  • Arduino uno
  • HC-SR04 Ultrasonic sensor.
  • Jumper wire set MM/FF/MF
  • The Windows application
  • Visual Studio to build your own windows app from the source.(optional)


C# Source: Download here

Arduino code:

#define echo 8
#define trigger 9
long duration,distance;

void setup() {
 Serial.begin (9600);
 pinMode(trigger, OUTPUT);
 pinMode(echo, INPUT);
 digitalWrite(trigger, LOW); 

void loop() {
 digitalWrite(trigger, LOW); 
 digitalWrite(trigger, HIGH);
 digitalWrite(trigger, LOW);
 duration = pulseIn(echo, HIGH);
 distance = duration/58;
 if ((distance >= 400) || (distance <= 2))
 distance = -1;

Good luck!

Follow me on youtube!

Danny van den Brande

Programmer, Game Designer, 3D Designer, Web Designer, Graphic Designer. Jup this website looks crappy.. :) But enjoy the projects! Everything you need is here.

Leave a Reply

Your email address will not be published. Required fields are marked *