Build Your Own Temperature Monitor With Meadow

Photo of jorgedevs

Made by jorgedevs

About the project

Learn how to make a temperature monitor using an LM35 temperature sensor and a ST7789 SPI display using Meadow. Foundation driver library.

Project info

Difficulty: Easy

Platforms: MicrosoftMeadowWilderness Labs

Estimated time: 1 hour

License: MIT license (MIT)

Items used in this project

Hardware components

LM35 - TEMPERATURE SENSOR TO-92 LM35 - TEMPERATURE SENSOR TO-92 x 1
Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM x 1
Jumper Wire Kit - 140pcs Jumper Wire Kit - 140pcs x 1
Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM x 1

Software apps and online services

Microsoft Visual Studio 2019 Microsoft Visual Studio 2019

Story

This project uses a LM35 temperature sensor and a ST7789 SPI display to make a simple room temperature monitor. Everything you need to build this project is included in the Wilderness Labs Meadow F7 w/Hack Kit Pro. We'll see how easy is to program these peripherals using Meadow.Foundation.

Meadow.Foundationa platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.

If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LEDproject to properly set up your development environment.

Step 1 - Assemble the circuit

Wire your project like this:

Fritzing Diagram for the Temperature Monitor

Fritzing Diagram for the Temperature Monitor

Fritzing Diagram for the Temperature Monitor

Step 2 - Create a Meadow Application project

Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it TemperatureMonitor.

Step 3 - Add the required NuGet packages

Windows

Right-click on your TemperatureMonitor project and click Manage NuGet Packages. In the Browse tab, search for Meadow.Foundation.Displays.Tft and click Install to add it to your project.

macOS

Alt-click on your TemperatureMonitor project in the Solution Explorer, and click Add => Add Nuget Package to open the NuGet Package window. Search for Meadow.Foundation.Displays.Tft and click Install to add it to your project.

Step 4 - Write the code for TemperatureMonitor

Copy the following code below:

using Meadow;using Meadow.Devices;using Meadow.Foundation;using Meadow.Foundation.Displays.Tft;using Meadow.Foundation.Graphics;using Meadow.Foundation.Sensors.Temperature;using Meadow.Hardware;using Meadow.Peripherals.Sensors.Atmospheric;using System;namespace TemperatureMonitor{    public class MeadowApp : App<F7Micro, MeadowApp>    {        Color[] colors = new Color[4]         {             Color.FromHex("#008500"),             Color.FromHex("#269926"),             Color.FromHex("#00CC00"),             Color.FromHex("#67E667")         };        AnalogTemperature analogTemperature;        St7789 st7789;        GraphicsLibrary graphics;        int displayWidth, displayHeight;        public MeadowApp()        {            Console.WriteLine("Initializing...");            analogTemperature = new AnalogTemperature(                device: Device,                analogPin: Device.Pins.A00,                sensorType: AnalogTemperature.KnownSensorType.LM35            );            analogTemperature.Updated += AnalogTemperatureUpdated;            var config = new SpiClockConfiguration(                6000,                 SpiClockConfiguration.Mode.Mode3);            st7789 = new St7789(                device: Device,                spiBus: Device.CreateSpiBus(                    Device.Pins.SCK, Device.Pins.MOSI, Device.Pins.MISO, config),                chipSelectPin: Device.Pins.D02,                dcPin: Device.Pins.D01,                resetPin: Device.Pins.D00,                width: 240, height: 240            );            displayWidth = Convert.ToInt32(st7789.Width);            displayHeight = Convert.ToInt32(st7789.Height);            graphics = new GraphicsLibrary(st7789);            graphics.Rotation = GraphicsLibrary.RotationType._270Degrees;            LoadScreen();            analogTemperature.StartUpdating();        }        void LoadScreen()        {            Console.WriteLine("LoadScreen...");            graphics.Clear();            int radius = 225;            int originX = displayWidth / 2;            int originY = displayHeight / 2 + 130;            graphics.Stroke = 3;            for (int i = 1; i < 5; i++)            {                graphics.DrawCircle(                    centerX: originX,                    centerY: originY,                    radius: radius,                    color: colors[i - 1],                    filled: true);                graphics.Show();                radius -= 20;            }            graphics.DrawLine(0, 220, 240, 220, Color.White);            graphics.DrawLine(0, 230, 240, 230, Color.White);            graphics.CurrentFont = new Font12x20();            graphics.DrawText(54, 130, "TEMPERATURE", Color.White);            graphics.Show();        }        void AnalogTemperatureUpdated(object sender, AtmosphericConditionChangeResult e)        {            float oldTemp = e.Old.Temperature / 1000;            float newTemp = e.New.Temperature / 1000;            graphics.DrawText(                x: 48, y: 160,                text: $"{oldTemp.ToString("##.#")}°C",                color: colors[colors.Length - 1],                scaleFactor: GraphicsLibrary.ScaleFactor.X2);            graphics.DrawText(                x: 48, y: 160,                text: $"{newTemp.ToString("##.#")}°C",                color: Color.White,                scaleFactor: GraphicsLibrary.ScaleFactor.X2);            graphics.Show();        }    }}

In the MeadowApp's constructor, its all about initializing the peripherals on our project, which in this case is the AnalogTemperature object, in which we make sure we're specifying its a LM35 sensor type. To make the sensor be active to read the temperature, we'll use the events and IObservable pattern found in Meadow.Core to read, poll and filter input from sensors automatically. In this case, we're registering the LM35 to an Updated event to constantly check the old and new values once we call the StartUpdating method.

We also initialize the ST7789 display, specifying which Pins are we connected and passing a SPI Configuration object to make sure we're connecting to the display in the right SPI mode. We're also using Meadow.Foundation's GraphicsLibrary to easily draw basic shapes and text for our UI.

In the LoadScreen method it will load all the UI to display the temperature, it starts by drawing four concentric circles, and a TEMPERATURE text as a title and two horizontal lines in the bottom of the screen.

Finally, in the AnalogTemperatureUpdated event handler, we'll simply draw the previous temperature value in the same color of the background to "erase" it, and we draw the new value right after, so we won't need to redraw the entire screen each time the event is raised.

Step 5 - Run the project

Click the Run button in Visual Studio. It should look like to the following GIF:

TemperatureMonitor project running

TemperatureMonitor project running

TemperatureMonitor project running

Check out Meadow.Foundation!

This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.

  • It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
  • The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
  • This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.

References

Schematics, diagrams and documents

TemperatureMonitor circuit diagram

Code

TemperatureMonitor complete project

You can check the TemperatureMonitor project in Meadow.Samples/Source/MeadowSamples/Projects/TemperatureMonitor

Credits

Leave your feedback...