2.2 Automatic Fan
-
Does your house have an air conditioner? What about your fridge - how does it stay at the right temperature? Both of these machines use a temperature sensor as an input to trigger actions.
We’re going to use a DHT11 Temperature and Humidity Sensor to detect the temperature around your micro:bit, and then use that to trigger actions in our code. -
It’s time to give our micro:bit some new features! When we want to expand beyond the inputs and outputs already built in to the micro:bit, we can attach it to another device to add even more features. One of those devices is the sensor:bit. The sensor:bit adds a range of new features to the micro:bit, including:
- A headphone jack for connecting headphones to your micro:bit.
- Additional power for controlling motors and servos.
- Extra pins for connecting to other devices (different kinds of inputs and outputs).
We'll mainly use the sensor:bit to enable us to plug in different kinds of sensors that aren’t directly compatible with the micro:bit. With a tool like the sensor:bit, you can make the micro:bit do just about anything!
- A headphone jack for connecting headphones to your micro:bit.
-
The micro:bit has 25 gold stripes on one side of the device. These are called pins. These pins are used to connect external inputs and outputs. A program can communicate using these external components by either checking the voltage of a pin in the case of an input, or by sending voltage through a pin in the case of an output. On the micro:bit, pins 0, 1 and 2 are labelled, with 3V power for components.
We’ll be using the sensor:bit to connect external inputs and outputs to the micro:bit. On the bottom of the sensor:bit, there are a series of yellow, red, and black pins. The micro:bit uses the yellow pins to read from or write to components. The red pins supply power to the component (3V), and the black pins are ground (GND).
When connecting components to this sensor:bit, it is important to match the wire colours to the red and black pins. The wire on the yellow pin can also sometimes be green or white, but red being for power and black being for ground is a convention in electronics and is important to remember.Above each pin on the sensor:bit is a number label. For most activities in which we use the sensor:bit, we’ll want to stick with pins 1 and 2. This is because many functions on the sensor:bit are also connected to some of these available pins. Pins 1 and 2 are not used by other on-board components, so won’t affect the micro:bit when used.
If more pins are needed, try pins 8, 12, or 16.
-
This lesson will be using a motor and fan. The motor can be attached to the sensor:bit board using one of the cables included in your micro:bit kit.
Notice how the red and black wires are connected to the red and black pins on the board, and the green wire is connected to the yellow pin.
When connecting components to the sensor:bit, make sure the red power pin is connected to the red wire, and the black ground pin is connected to the black wire. The yellow pin can be connected to a wire of other colours than yellow. Green, blue and white are common alternative colours.
The motor will spin the fan blades when power is directed to the device. We'll use code blocks in MakeCode to send power to the motor.
-
External inputs and outputs can be connected to a micro:bit or sensor:bit, but physically connecting a component to a device is only the first step. For actually communicating with a component, we can use the
digital read pin and
digital write pin blocks. These code blocks are found in the the Pins menu in MakeCode, which is under the Advanced menu.The digital write pin block is used to overwrite the current value of the selected pin with the given value. We can use this to turn components and circuits on or off, or send data.
Here are some tips on how to use the
digital write block:- The values you can read or write are 0 and 1.
- The digital write pin block can be used to turn components on or off: 1 is on, and 0 is off.
- The values you can read or write are 0 and 1.
-
The Challenge
- When you press the A button, the fan turns on.
- When you press the B button, the fan turns off.
Required Devices
- 1 micro:bit
- 1 sensor:bit
- 1 motor + fan
Suggested Blocks
- on button pressed
- digital write pin
Bonus Challenges
- Can you make the motorised fan turn on when it receives a radio message from another micro:bit?
-
Extensions are a fast and simple way to bring new code blocks and functionality into your projects. Many different types of specialised sensors and outputs already have micro:bit extensions made and ready to use, so all we have to do is import their extensions to add their code blocks to your project.
In this lesson, we will be using the DHT11_DHT22 extension. This extension will give us code blocks that work with the DHT11 Temperature and Humidity Sensor.
Once the extension has been installed, you’ll see the DHT11/DHT22 menu appear in MakeCode.
-
Temperature is a measure of heat, whereas humidity is a measure of the amount of water vapour that is in the air. These are two different but connected factors of our environment, and can affect nature in different ways.
What we commonly refer to as condensation is actually the work of humidity in our environment. If you have a warm shower, the temperature of the water is different to the temperature of the surface that it touches.
In the case of a glass shower door, or a glass window in the bathroom, the pane of glass is a different temperature to the water droplets and water vapour in the bathroom, which causes the glass to 'fog'. If you touched the glass, the glass would still be cold. This reaction is an example of a humid room where there is a high concentration of warm water vapour in the air.
In the case of a pane of glass that is exposed to a hot temperature, the heat of the glass increases. The glass won't 'fog', but if you were to touch the glass, you might burn your finger. If the heat from the sun is focused on a pane of glass, then its temperature will increase. This reaction is an example of an increase in temperature and heat, rather than humidity.
-
The DHT11 is a temperature and humidity sensor. Unlike the motor and fan unit we used earlier, this sensor communicates with the micro:bit using its own format. Simple digital reads won’t result in us gaining any meaningful data. To solve this, we’ll be using a MakeCode extension.
The DHT11 extension contains a number of blocks relating to this sensor. The most important block is the
Query DHT11 block.This block communicates with the DHT11 on the selected pin, so make sure that the pin number matches the pin to which the DHT11 is connected. Once the Query DHT11 block has been run, the read humidity and
read temperature blocks will contain accurate humidity and temperature values taken from the sensor.When using this block, make sure that you have the DHT11 selected (the DHT22 is a different sensor that we don't use).
Also, make sure that the Pin pull up line of the code block is set to true, or the information from the sensor won't be read.
Don't worry about the Serial output and
Wait 2 sec after query sections of this block. These relate to the DHT22, which is a sensor we are not using, and these lines won't affect the readout of our DHT11 sensor.In this lesson, we will only be sensing the temperature of our room, and will not need to use the humidity functionality.
Here are some tips on how to use the DHT11 Temperature/Humidity Sensor:
- Make sure that the red wire goes to the red power pin, and the black wire goes to the black ground pin.
- The yellow signal pin can be connected with a wire that isn’t yellow. Green, blue and white are all common colours for the signal wire.
- Use the Query DHT11 block to communicate with the DHT11 sensor.
- Once the Query DHT11 block has been run, use the read temperature and read humidity blocks to get data from the sensor.
- Make sure that the red wire goes to the red power pin, and the black wire goes to the black ground pin.
-
The Challenge
Show the current temperature in the room as a string on the micro:bit display.
Required Devices
- 1 micro:bit
- 1 sensor:bit
- 1 DHT11 temperature-humidity sensor
Suggested Blocks
- forever
- show string
- query DHT11
- read temperature
- last query successful?
- if true then
Hints
- Make sure that the pin number on the Query DHT11 block is the same pin number that your sensor is connected to.
- When using this block, make sure that you have the DHT11 selected (the DHT22 is a different sensor).
- Make sure that the "Pin Pull Up" has true selected, or the information from the sensor won't be read.
Bonus Challenge
- Add code that keeps track of the minimum and maximum temperatures recorded by the DHT11, and prints them to the screen when A and B are pressed.
-
The Challenge
- When the temperature is high, the fan turns on.
- When the temperature is low, the fan turns off.
- The current temperature is always visible on the display.
Required Devices
- 1 micro:bit
- 1 sensor:bit
- 1 DHT11 Temperature/Humidity Sensor module
- 1 motor + fan unit
Suggested Blocks
- forever
- show number
- query DHT11
- read temperature
- last query successful?
- if true then else
- 0 > 0
- digital write pin
Hints
- You might need to experiment to figure out a temperature that works well for your variable. Try to find a temperature that's slightly below the temperature of the room that you're in.
- You can warm up the temperature sensor by holding your hand on it. You can cool it down by fanning it with a piece of paper (or pointing the micro:bit fan at it if it's turned on).
Bonus Challenge
- Can you set the fan to turn on and off based on a countdown timer? Try also to set the fan to turn on and off at regular timed intervals.
-
In this lesson we met new devices with the sensor:bit, motor + fan, and DHT11 Temperature and Humidity Sensor. We learned how to read from and write to digital pins, how to use MakeCode Extensions, and how to program an automatic fan based on the ambient temperature.
In the next lesson, we'll learn how make a countdown timer, how to use a servo motor, and how to use a motion sensor, then we'll combine that knowledge to build an automatic pet feeder.