Home Code LM35 temperature data to C Sharp

LM35 temperature data to C Sharp

by shedboy71

This example was just a modification our LM35 sensor example at http://www.arduinolearning.com/code/lm35-temperature-sensor.php. This time we will create an app to read the value sent rather than using the serial monitor.

Arduino Code

Slight change in that we removed some of the serial output and just sent out the temperature in celsius

[codesyntax lang=”cpp”]

int outputpin= 0;
//this sets the ground pin to LOW and the input voltage pin to high
void setup()
{
Serial.begin(9600);
}

//main loop
void loop()
{
int rawvoltage= analogRead(outputpin);
float millivolts= (rawvoltage/1024.0) * 5000;
float celsius= millivolts/10;
Serial.println((int)celsius);
delay(1000);

}

[/codesyntax]

C# App

Again we are using C#, this time I created the app in Visual Studio 2012. Link later on.

C Sharp temp form

C Sharp temp form

Here is the main code, same principle as before add a label and a text box to a form and also a serial port control from the toolbox. Note teh com port is hardcoded in this example to COM6, you'll probably want to change taht

[codesyntax lang=”csharp”]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace SHT21Output
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            serialPort1.BaudRate = 9600;
            serialPort1.PortName = "COM6";
            serialPort1.Open();
            serialPort1.DataReceived += serialPort1_DataReceived;
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string line = serialPort1.ReadLine();
            this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
        }

        private delegate void LineReceivedEvent(string line);
        private void LineReceived(string line)
        {

            txtTemperature.Text = line + " degrees ";
        }
        
        
    }
}

[/codesyntax]

 

Links

VS2012 files and arduino sketch are available from https://github.com/arduinolearning/Interfacing/tree/master/Arduino%20RGB

Share

You may also like