C# with Arduino interfacing to read temperature

Download Report

Transcript C# with Arduino interfacing to read temperature

C# with Arduino interfacing to
read temperature
Equipment:
1: LM35 sensor
2: Arduino
3: C sharp program
C# CODE
• private void btnShowTemperature_Click(object sender, EventArgs e)
• { try
•
{ if (serialPort1.IsOpen)
•
{ //get Temperature from serial port sent by Arduino
•
int temperature = (int)(Math.Round(Convert.ToDecimal(
•
serialPort1.ReadLine()), 0));
•
txtTemperature.Text = temperature.ToString();
•
//draw temperature in the thermometer
•
DrawTemperatureLevel(temperature); }
•
}
•
catch (Exception ex)
•
{
•
•
• }
MessageBox.Show(ex.Message);
}
A
•
#define sensorPin 0 //A0 Pin
•
int sensorValue = 0;
•
float celsius = 0;
•
void setup() {
•
•
Serial.begin(9600); //start Serial Port }
void loop() {
•
getTemperature();
•
//send celsious value to Port and read from C#
•
Serial.println(celsius);
•
delay(1000);
•
}
•
void getTemperature(){
•
//get sensorPin voltage
•
sensorValue = analogRead(sensorPin);
•
//convert voltage to celsius degree
•
celsius = (sensorValue * 0.0049) *100;
•
}