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

Digital Thermometer using c# and a microcontroller

, 25 Jul 2009 CPOL
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 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
Follow on   Google+

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... Pinmemberpatodi17-Jul-13 0:14 
AnswerRe: error while running... PinmemberHiteshSharma22-Aug-13 21:34 
GeneralMy vote of 5 Pinprofessionaldpalash1-Jul-13 1:44 
QuestionNice one PinmemberMember 880732810-Apr-12 7:53 
GeneralMy vote of 5 PinmemberAbinash Bishoyi30-Mar-12 2:38 
GeneralMy vote of 5 PinmemberP1119r1m5-Nov-11 8:23 
Questionnegative thermometer [modified] Pinmemberunbeliever124-Aug-11 1:58 
Questioncompliments sir Pinmemberbenbas24-Jul-11 7:25 
GeneralMy vote of 5 PinmemberJF201520-Jan-11 3:03 
GeneralMy vote of 5 Pinmemberthatraja9-Dec-10 20:27 
GeneralRe: My vote of 5 PinmemberHiteshSharma13-Dec-10 19:03 
QuestionHow to write programme to Micro Controller Pinmemberzain_ali8-Dec-10 2:46 
AnswerRe: How to write programme to Micro Controller PinmemberHiteshSharma9-Dec-10 4:39 
GeneralPhidgets for Data Input Pinmemberdmbrider22-Dec-09 11:36 
GeneralNice article Pinmemberthompsons9-Dec-09 7:25 
GeneralRe: Nice article PinmemberHiteshSharma9-Dec-09 7:52 
QuestionHow can a microcontroller (89S52,or any) to measure the beats per minute of a pulse circuit PinmemberMember 407735121-Oct-09 9:57 
AnswerRe: How can a microcontroller (89S52,or any) to measure the beats per minute of a pulse circuit PinmemberHiteshSharma22-Oct-09 6:03 
QuestionHow can i display data from microcontroller to Pc using serialport PinmemberMember 407735127-Aug-09 10:07 
AnswerRe: How can i display data from microcontroller to Pc using serialport Pinmemberviaducting19-Jun-12 3:44 
Questionreading data from a microcontroller (89s52) via serialport [modified] PinmemberMember 407735127-Aug-09 10:06 
AnswerRe: reading data from a microcontroller (89s52) via serialport PinmemberHiteshSharma27-Aug-09 13:00 
QuestionRe: reading data from a microcontroller (89s52) via serialport PinmemberMember 407735129-Aug-09 3:40 
AnswerRe: reading data from a microcontroller (89s52) via serialport PinmemberHiteshSharma29-Aug-09 4:39 
QuestionRe: reading data from a microcontroller (89s52) via serialport PinmemberMember 407735129-Aug-09 10:18 
AnswerRe: reading data from a microcontroller (89s52) via serialport PinmemberHiteshSharma29-Aug-09 21:48 
QuestionRe: reading data from a microcontroller (89s52) via serialport PinmemberMember 407735130-Aug-09 10:44 
AnswerRe: reading data from a microcontroller (89s52) via serialport PinmemberHiteshSharma31-Aug-09 5:08 
QuestionRe: reading data from a microcontroller (89s52) via serialport PinmemberMember 407735131-Aug-09 2:39 
QuestionRe: reading data from a microcontroller (89s52) via serialport PinmemberMember 40773512-Sep-09 10:43 
GeneralReading data from serialport to four and eight textbox using c# PinmemberMember 407735127-Aug-09 9:57 
GeneralRe: Reading data from serialport to four and eight textbox using c# PinmemberHiteshSharma27-Aug-09 12:58 
QuestionRe: Reading data from serialport to four and eight textbox using c# PinmemberMember 407735129-Aug-09 3:32 
GeneralThis was wonderful Pinmembervirdigs18-Aug-09 1:06 
GeneralRe: This was wonderful PinmemberHiteshSharma19-Aug-09 23:04 
GeneralGood Stuff ! PinmemberMubi | www.mrmubi.com16-Aug-09 6:15 
GeneralRe: Good Stuff ! PinmemberHiteshSharma16-Aug-09 7:06 
GeneralFantastic! PinmemberSharath C V10-Aug-09 21:41 
GeneralRe: Fantastic! PinmemberHiteshSharma10-Aug-09 22:24 
Generalgood job man . Pinmemberzzx_1234569-Aug-09 21:33 
QuestionWhat graphics program was used? Pinmembersynchrotron6-Aug-09 13:15 
AnswerRe: What graphics program was used? PinmemberHiteshSharma6-Aug-09 20:12 
GeneralGood article PinmemberCharles Putney5-Aug-09 0:59 
GeneralRe: Good article PinmemberHiteshSharma5-Aug-09 8:16 
QuestionUse all 8 ADC ports Pinmemberjoelcarroll4-Aug-09 7:58 
AnswerRe: Use all 8 ADC ports PinmemberHiteshSharma4-Aug-09 8:35 
AnswerRe: Use all 8 ADC ports PinmemberHiteshSharma4-Aug-09 9:26 
GeneralGood idea, but PinmemberDiscoJimmy4-Aug-09 7:43 
GeneralRe: Good idea, but PinmemberHiteshSharma4-Aug-09 8:34 
GeneralSerial ports are not so popular any more so... PinmemberJefferys4-Aug-09 6: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
Web04 | 2.8.150128.1 | Last Updated 26 Jul 2009
Article Copyright 2009 by HiteshSharma
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid