Click here to Skip to main content
11,705,098 members (60,312 online)
Click here to Skip to main content

Digital Thermometer using c# and a microcontroller

, 25 Jul 2009 CPOL 276.4K 5.9K 215
Rate this:
Please Sign up or sign in to vote.
microcontroller senses temperature using a sensor and transmits it to pc through serial port where it is displayed graphically on screen by an application in c#
This is an old version of the currently published article.

Introduction

 its a Digital Thermometer application which reads reading from serial port which are sent there by a hardware which calculates temperature frequently and transmits the readings to pc at a baud rate of 9600 bps    

 <img src="DigitalThermometer/thermometer.png"> 

Using the code 

First of all lets in the form load event initialise the serial port: 

			form1_load(object sender,eventargs e) 
{
portname = "COM4"; //global string variable
            parity = Parity.None; //global System.IO.Ports.Parity variable;
            BaudRate = 9600;      //global int variable   
            stopbits = StopBits.Two;    //global StopBits enum variable
            databits = 8;               //global int variable
              //global SerialPort variable
            port = new System.IO.Ports.SerialPort(portname);
            port.Parity = parity;
            port.BaudRate = BaudRate;
            port.StopBits = stopbits;
            port.DataBits = databits;
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.Open();
 }           
	 <span class="Apple-style-span" style="font-family: Verdana; font-size: 16px; "> </span>

Next we write the code to collect readings from serial port, calculating temperature and then converting it to proper angle through which the arm is to be rotated

<span class="Apple-style-span" style="font-family: Verdana; font-size: 13px; white-space: normal; "><pre lang="cs">void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            port.Read(bt, 0, 1);
            sum += Convert.ToDouble(bt[0]) *voltage*100/255;
            
            count++;
            if (count == 100)
            {
                InitialTemp = temp;
                temp = sum / 100;
                sum = 0;
                count = 0;
                double angle = 0;
                if ((Math.Floor(temp) > 30 && Math.Floor(temp) <= 40) || (Math.Floor(temp) < 70 && Math.Floor(temp) > 60))
                {
                    if (Math.Floor(temp) <= 40)
                        angle = -65 + 3 * ((temp - 1) % 10) + 3;
                    else
                    {
                        angle = 35 + 3 * ((temp - 1) % 10) + 3;
                    }

                }
                else if ((Math.Floor(temp) > 40 && Math.Floor(temp) <= 50) || (Math.Floor(temp) <= 60 && Math.Floor(temp) >= 50))
                {
                    //if (temp == 60) System.Diagnostics.Debugger.Break();
                    if (Math.Floor(temp) <= 50)
                        angle = -35 + 35 * ((temp - 1) % 10) / 10 + 3.5;
                    else
                    {
                        //if (temp == 50)
                        //    System.Diagnostics.Debugger.Break();
                        angle = 35 * ((temp - 1) % 10) / 10 + 3.5;
                    }
                }
                else
                {
                    if (Math.Floor(temp) <= 30)
                        angle = -125 + 2 * temp;
                    else
                    {
                        angle = 65 + 2 * (temp - 70);
                    }
                }
                if (AnimationAllowed)
                    Animate(CurrentAngle, angle);
                
            }
            //throw new NotImplementedException();
            
        } 

 

and then finally the Form1_Paint event which paints the form : 

private void Form1_Paint(object sender, PaintEventArgs e)
       {
           if (firsttime)
               if (AnimationAllowed)
                   AnimationAllowed = false;
           //t1.Enabled = false;
           try
           {
               surface.FillRectangle(Brushes.LightSteelBlue, DrawingRectangle);
               Image img = new Bitmap(Properties.Resources.speedometer, this.Size);
               Graphics ImageGraphics = Graphics.FromImage(img);
               Image hand = new Bitmap(Properties.Resources.MinuteHand);

               //handgraphics.TranslateTransform(hand.Width / 2, hand.Height / 2);

               //ImageGraphics.DrawImage(hand, new Point(this.Width / 2 -10, 40));
               surface.DrawImageUnscaled(img, new Point(0, 0));

               //e.Graphics.TranslateTransform(0,0);
               surface.TranslateTransform(this.Width / 2f, this.Height / 2f);

               surface.RotateTransform((float)CurrentAngle);


               surface.DrawImage(hand, new Point(-10, -this.Height / 2 + 40));


               string stringtemp = displaytemp.ToString(); //InitialTemp.ToString(); //temp.ToString();
               stringtemp = stringtemp.Length > 5 ? stringtemp.Remove(5, stringtemp.Length - 5) : stringtemp;
               Font fnt = new Font("Arial", 20);
               SizeF siz = surface.MeasureString(stringtemp, fnt);
               surface.ResetTransform();


               LinearGradientBrush gd = new LinearGradientBrush(new Point(0,(int)siz.Height + 20), new Point((int)siz.Width,0), Color.Red, Color.Lavender);
               surface.DrawString(stringtemp, fnt, gd, new PointF(DrawingRectangle.Width / 2 - siz.Width / 2, 70));


               surface.DrawEllipse(Pens.LightGray, DrawingRectangle);


               buff.Render();
               if (firsttime)
               {
                   firsttime = false;
                   AnimationAllowed = true;
               }
           }
           catch (InvalidOperationException)
           {


           }

           //MessageBox.Show(count.ToString());
           //t1.Enabled = true;
       }

Points of Interest

this tells how we can read serial port data and then use it in applications.

the hardware is also my self creation.  

For Hardware details, leave me a message 

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

HiteshSharma
India India
I'm a ruby developer in LinkedIn these days but used to code in C# some years back for my hobby projects.

You may also be interested in...

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
Questionerror while running... Pin
patodi16-Jul-13 23:14
memberpatodi16-Jul-13 23:14 
AnswerRe: error while running... Pin
HiteshSharma22-Aug-13 20:34
memberHiteshSharma22-Aug-13 20:34 
GeneralMy vote of 5 Pin
dpalash1-Jul-13 0:44
professionaldpalash1-Jul-13 0:44 
QuestionNice one Pin
Member 880732810-Apr-12 6:53
memberMember 880732810-Apr-12 6:53 
GeneralMy vote of 5 Pin
Abinash Bishoyi30-Mar-12 1:38
memberAbinash Bishoyi30-Mar-12 1:38 
GeneralMy vote of 5 Pin
P1119r1m5-Nov-11 7:23
memberP1119r1m5-Nov-11 7:23 
Questionnegative thermometer [modified] Pin
unbeliever124-Aug-11 0:58
memberunbeliever124-Aug-11 0:58 
Questioncompliments sir Pin
benbas24-Jul-11 6:25
memberbenbas24-Jul-11 6:25 
GeneralMy vote of 5 Pin
JF201520-Jan-11 2:03
memberJF201520-Jan-11 2:03 
GeneralMy vote of 5 Pin
thatraja9-Dec-10 19:27
memberthatraja9-Dec-10 19:27 
GeneralRe: My vote of 5 Pin
HiteshSharma13-Dec-10 18:03
memberHiteshSharma13-Dec-10 18:03 
QuestionHow to write programme to Micro Controller Pin
zain_ali8-Dec-10 1:46
memberzain_ali8-Dec-10 1:46 
AnswerRe: How to write programme to Micro Controller Pin
HiteshSharma9-Dec-10 3:39
memberHiteshSharma9-Dec-10 3:39 
GeneralPhidgets for Data Input Pin
dmbrider22-Dec-09 10:36
memberdmbrider22-Dec-09 10:36 
GeneralNice article Pin
thompsons9-Dec-09 6:25
memberthompsons9-Dec-09 6:25 
GeneralRe: Nice article Pin
HiteshSharma9-Dec-09 6:52
memberHiteshSharma9-Dec-09 6:52 
QuestionHow can a microcontroller (89S52,or any) to measure the beats per minute of a pulse circuit Pin
Member 407735121-Oct-09 8:57
memberMember 407735121-Oct-09 8:57 
AnswerRe: How can a microcontroller (89S52,or any) to measure the beats per minute of a pulse circuit Pin
HiteshSharma22-Oct-09 5:03
memberHiteshSharma22-Oct-09 5:03 
QuestionHow can i display data from microcontroller to Pc using serialport Pin
Member 407735127-Aug-09 9:07
memberMember 407735127-Aug-09 9:07 
AnswerRe: How can i display data from microcontroller to Pc using serialport Pin
viaducting19-Jun-12 2:44
memberviaducting19-Jun-12 2:44 
Questionreading data from a microcontroller (89s52) via serialport [modified] Pin
Member 407735127-Aug-09 9:06
memberMember 407735127-Aug-09 9:06 
AnswerRe: reading data from a microcontroller (89s52) via serialport Pin
HiteshSharma27-Aug-09 12:00
memberHiteshSharma27-Aug-09 12:00 
QuestionRe: reading data from a microcontroller (89s52) via serialport Pin
Member 407735129-Aug-09 2:40
memberMember 407735129-Aug-09 2:40 
AnswerRe: reading data from a microcontroller (89s52) via serialport Pin
HiteshSharma29-Aug-09 3:39
memberHiteshSharma29-Aug-09 3:39 
QuestionRe: reading data from a microcontroller (89s52) via serialport Pin
Member 407735129-Aug-09 9:18
memberMember 407735129-Aug-09 9:18 
AnswerRe: reading data from a microcontroller (89s52) via serialport Pin
HiteshSharma29-Aug-09 20:48
memberHiteshSharma29-Aug-09 20:48 
QuestionRe: reading data from a microcontroller (89s52) via serialport Pin
Member 407735130-Aug-09 9:44
memberMember 407735130-Aug-09 9:44 
AnswerRe: reading data from a microcontroller (89s52) via serialport Pin
HiteshSharma31-Aug-09 4:08
memberHiteshSharma31-Aug-09 4:08 
QuestionRe: reading data from a microcontroller (89s52) via serialport Pin
Member 407735131-Aug-09 1:39
memberMember 407735131-Aug-09 1:39 
QuestionRe: reading data from a microcontroller (89s52) via serialport Pin
Member 40773512-Sep-09 9:43
memberMember 40773512-Sep-09 9:43 
GeneralReading data from serialport to four and eight textbox using c# Pin
Member 407735127-Aug-09 8:57
memberMember 407735127-Aug-09 8:57 
GeneralRe: Reading data from serialport to four and eight textbox using c# Pin
HiteshSharma27-Aug-09 11:58
memberHiteshSharma27-Aug-09 11:58 
QuestionRe: Reading data from serialport to four and eight textbox using c# Pin
Member 407735129-Aug-09 2:32
memberMember 407735129-Aug-09 2:32 
GeneralThis was wonderful Pin
virdigs18-Aug-09 0:06
membervirdigs18-Aug-09 0:06 
GeneralRe: This was wonderful Pin
HiteshSharma19-Aug-09 22:04
memberHiteshSharma19-Aug-09 22:04 
GeneralGood Stuff ! Pin
Mubi | www.mrmubi.com16-Aug-09 5:15
memberMubi | www.mrmubi.com16-Aug-09 5:15 
GeneralRe: Good Stuff ! Pin
HiteshSharma16-Aug-09 6:06
memberHiteshSharma16-Aug-09 6:06 
GeneralFantastic! Pin
Sharath C V10-Aug-09 20:41
memberSharath C V10-Aug-09 20:41 
GeneralRe: Fantastic! Pin
HiteshSharma10-Aug-09 21:24
memberHiteshSharma10-Aug-09 21:24 
Generalgood job man . Pin
zzx_1234569-Aug-09 20:33
memberzzx_1234569-Aug-09 20:33 
QuestionWhat graphics program was used? Pin
synchrotron6-Aug-09 12:15
membersynchrotron6-Aug-09 12:15 
AnswerRe: What graphics program was used? Pin
HiteshSharma6-Aug-09 19:12
memberHiteshSharma6-Aug-09 19:12 
GeneralGood article Pin
Charles Putney4-Aug-09 23:59
memberCharles Putney4-Aug-09 23:59 
GeneralRe: Good article Pin
HiteshSharma5-Aug-09 7:16
memberHiteshSharma5-Aug-09 7:16 
QuestionUse all 8 ADC ports Pin
joelcarroll4-Aug-09 6:58
memberjoelcarroll4-Aug-09 6:58 
AnswerRe: Use all 8 ADC ports Pin
HiteshSharma4-Aug-09 7:35
memberHiteshSharma4-Aug-09 7:35 
AnswerRe: Use all 8 ADC ports Pin
HiteshSharma4-Aug-09 8:26
memberHiteshSharma4-Aug-09 8:26 
GeneralGood idea, but Pin
DiscoJimmy4-Aug-09 6:43
memberDiscoJimmy4-Aug-09 6:43 
GeneralRe: Good idea, but Pin
HiteshSharma4-Aug-09 7:34
memberHiteshSharma4-Aug-09 7:34 
GeneralSerial ports are not so popular any more so... Pin
Jefferys4-Aug-09 5:18
memberJefferys4-Aug-09 5:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 26 Jul 2009
Article Copyright 2009 by HiteshSharma
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid