In this article, I am going to show how we can create a simple temperature gauge. By the end of this article, you will be able to make your own simple temperature gauge. I suggest you go through the following articles where I have covered how to read temperature and how to drive servo using Micro:Bit; I am going to combine these two things and make a temperature gauge. So, if you go through these two articles, you will have a clear idea of what I am doing and how I am doing it.
So, let's start with the designing part and then we will go to the coding part. What you need to do is to cut the cardboard paper and use a compass to draw a half circle on it and label it with the number range you want, as in the below image. And what else you need to do is make a hole so that we will be able to use something like a needle for pointing purposes.
Now, let us see the connection part. The connection is same as we have done in the previous article How to Drive servo; here's how to do it.
Just like this:
That's all. Now, let us see the coding part. So for the coding part, we will use block coding. Go to makecode.microbit.org and create a new project.
Here also, we're going to use temperature variable for reading the temperature and we will apply some logic also for decision making purposes. Follow the steps and you will get it.
Go to makecode.microbit.org and create a new project and delete the start block; however, if you don't delete it, you will not face any problem.
Go to the variable and choose the "set item" block, place it after the "forever" block, and rename the item variable to temperature.
Now, go to input and choose a temperature block and attach it to "set temperature to variable" like this.
Now, go to the Logic block and choose IF THEN ELSE and 0=0 block and place it after "set item to" block as follows:
What we need to do now is to replace true of iF then ELSE block to 0=0 block and place temperature variable there. We're going to set room temperature to something like 20 degrees.
So according to the above code, if the temperature is less than 20, then we're assigning/setting the temperature variable to 20. If not and the temperature is more than 35 degrees, then we're setting the temperature to 35. You can choose any number you want based on the temperature at your place.
Now, we need to map these values to our servo so that according to our values, the servo will rotate and as shown above, we have one meter so we can see the temperature and servo adjusting according to temperature. It will look like a simple temperature gauge. To do so:
Go to pins and then choose map block and servo write block.
Replace 180 in servo write pin to map block. And then set values, meaning we need to map the temperature because according to temperature value only we need to rotate or adjust the servo. So copy temperature block and attach it to map block. 0 should be replaced by temperature. And we need to set low value and high value. So the low value is 20 (see if then else block) and the high value is 35 (see if then else block). So set from low=20 and from high=35. Above first two values are for setting the value and the next two values are used to give the angle to the servo. So set it to 0 and 180. So in this case, if the temperature is less than 20 then the servo will be down, meaning at 0 degrees, and if the temperature is more than 35 then it will move to 180 degrees, like this:
That's all -- now just download and copy to the MICROBIT DRIVE. And try to heat the CPU and you will see th servo adjusting according to temperature.
Get the code HERE
You can see a demo HERE . In this demo I have used different values and then adjusted the servo according to those values.
I hope it helps.