Using Arduino Sensors with a BBC micro:Bit
or
How to get Arduino to Talk to BBC micro:Bit
I can hear you!
"Why would you use an Arduino to connect a sensor to a micro:Bit? JUST USE THE ARDUINO!
You're right! You're right! No argument there.
But maybe there's some NEED, like wanting school kids to use micro:Bits for a purpose and you don't want to re-buy micro:Bit versions of Arduino sensors that work perfectly well and that are DIRT CHEAP!!! Or maybe you want to have the Arduino communicate with the micro:Bit?
The basic premise is:
1. Write an Arduino sketch to read your sensor.
2. Build a formatted string that will be sent to the micro:Bit via a serial communications connection.
3. Connect the Arduino and the micro:Bit together with 2 wires. (Or 3 wires if you want bidirectional communication)
4. Build a very simple makeCode sketch for the micro:Bit to read the data string from the Arduino.
In this example, we'll use a DHT11 temperature/humidity sensor.
Hardware Hookup:
Arduino to DHT11
+5V to VCC
GND to GND
pin 2 to Data
Arduino to micro:Bit
pin 11 to pin 1
(If you want bidirectional communications, connect Arduino pin 10 to micro:Bit pin 0)
GND to GND
Here's the Arduino sketch:
// Interface DHT11 through Arduino to micro:Bit
// 2018 - Gord Payne
#include "DHT.h"
float h, t;
DHT DHT_sens(2, DHT11); //datapin sensor to pin 2 Arduino
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
String theReading = "rH"; // data record that will be concatenated with temp and humidity values in the loop
void setup()
{
DHT_sens.begin();
Serial.begin (9600);
mySerial.begin(9600);
}
void loop() {
h = DHT_sens.readHumidity();
t = DHT_sens.readTemperature();
theReading = theReading + String(h) + "% T" + String(t) + "C"; // add humidity and temperature readings to the data string
Serial.println(theReading); // just for checking your programs work, not needed
mySerial.println(theReading);// send the readings to the micro:Bit
delay (10000); // pause 10 seconds
theReading = "rH";// clear string for next reading
}
"Why would you use an Arduino to connect a sensor to a micro:Bit? JUST USE THE ARDUINO!
You're right! You're right! No argument there.
But maybe there's some NEED, like wanting school kids to use micro:Bits for a purpose and you don't want to re-buy micro:Bit versions of Arduino sensors that work perfectly well and that are DIRT CHEAP!!! Or maybe you want to have the Arduino communicate with the micro:Bit?
The basic premise is:
1. Write an Arduino sketch to read your sensor.
2. Build a formatted string that will be sent to the micro:Bit via a serial communications connection.
3. Connect the Arduino and the micro:Bit together with 2 wires. (Or 3 wires if you want bidirectional communication)
4. Build a very simple makeCode sketch for the micro:Bit to read the data string from the Arduino.
In this example, we'll use a DHT11 temperature/humidity sensor.
Hardware Hookup:
Arduino to DHT11
+5V to VCC
GND to GND
pin 2 to Data
Arduino to micro:Bit
pin 11 to pin 1
(If you want bidirectional communications, connect Arduino pin 10 to micro:Bit pin 0)
GND to GND
Here's the Arduino sketch:
// Interface DHT11 through Arduino to micro:Bit
// 2018 - Gord Payne
#include "DHT.h"
float h, t;
DHT DHT_sens(2, DHT11); //datapin sensor to pin 2 Arduino
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
String theReading = "rH"; // data record that will be concatenated with temp and humidity values in the loop
void setup()
{
DHT_sens.begin();
Serial.begin (9600);
mySerial.begin(9600);
}
void loop() {
h = DHT_sens.readHumidity();
t = DHT_sens.readTemperature();
theReading = theReading + String(h) + "% T" + String(t) + "C"; // add humidity and temperature readings to the data string
Serial.println(theReading); // just for checking your programs work, not needed
mySerial.println(theReading);// send the readings to the micro:Bit
delay (10000); // pause 10 seconds
theReading = "rH";// clear string for next reading
}
Here's the micro:Bit sketch (yes, it's that simple)
The 'Humidity' variable is the string that contains the string 'theReading' sent from the Arduino. In this case it contains the relative humidity and the temperature.