Click here to Skip to main content
Click here to Skip to main content

Digital Thermometer using c# and a microcontroller

By , 25 Jul 2009
 
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)

About the Author

HiteshSharma
India India
Member
I am a siebel developer by profession and a C#, ASP.Net, Javascript, C, C++ developer by Hobby. I code mostly for fun and usually code to create utilities and applications to enhance, improve and ease out my work while working on my computer. I try to make my computer a better place to code. I work on electronics as well and like to create hardware which may integrate with my computer. Now a days i code mostly for web and spend more and more of my time on javascript.
 
visit my blog at: http://msphitesh.blogspot.in

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

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.
 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNice onememberMember 880732810 Apr '12 - 6:53 
GeneralMy vote of 5memberAbinash Bishoyi30 Mar '12 - 1:38 
GeneralMy vote of 5memberP1119r1m5 Nov '11 - 7:23 
Questionnegative thermometer [modified]memberunbeliever124 Aug '11 - 0:58 
Questioncompliments sirmemberbenbas24 Jul '11 - 6:25 
GeneralMy vote of 5memberJF201520 Jan '11 - 2:03 
GeneralMy vote of 5memberthatraja9 Dec '10 - 19:27 
GeneralRe: My vote of 5memberHiteshSharma13 Dec '10 - 18:03 
QuestionHow to write programme to Micro Controllermemberzain_ali8 Dec '10 - 1:46 
AnswerRe: How to write programme to Micro ControllermemberHiteshSharma9 Dec '10 - 3:39 
GeneralPhidgets for Data Inputmemberdmbrider22 Dec '09 - 10:36 
GeneralNice articlememberthompsons9 Dec '09 - 6:25 
GeneralRe: Nice articlememberHiteshSharma9 Dec '09 - 6:52 
QuestionHow can a microcontroller (89S52,or any) to measure the beats per minute of a pulse circuitmemberMember 407735121 Oct '09 - 8:57 
AnswerRe: How can a microcontroller (89S52,or any) to measure the beats per minute of a pulse circuitmemberHiteshSharma22 Oct '09 - 5:03 
QuestionHow can i display data from microcontroller to Pc using serialportmemberMember 407735127 Aug '09 - 9:07 
AnswerRe: How can i display data from microcontroller to Pc using serialportmemberviaducting19 Jun '12 - 2:44 
Questionreading data from a microcontroller (89s52) via serialport [modified]memberMember 407735127 Aug '09 - 9:06 
AnswerRe: reading data from a microcontroller (89s52) via serialportmemberHiteshSharma27 Aug '09 - 12:00 
QuestionRe: reading data from a microcontroller (89s52) via serialportmemberMember 407735129 Aug '09 - 2:40 
AnswerRe: reading data from a microcontroller (89s52) via serialportmemberHiteshSharma29 Aug '09 - 3:39 
QuestionRe: reading data from a microcontroller (89s52) via serialportmemberMember 407735129 Aug '09 - 9:18 
AnswerRe: reading data from a microcontroller (89s52) via serialportmemberHiteshSharma29 Aug '09 - 20:48 
QuestionRe: reading data from a microcontroller (89s52) via serialportmemberMember 407735130 Aug '09 - 9:44 
AnswerRe: reading data from a microcontroller (89s52) via serialportmemberHiteshSharma31 Aug '09 - 4:08 

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

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