individual assignment:
measure something: add a sensor to a microcontroller board
that you have designed and read it
I used breaboard, ESP8266, Micro:bit etc. to add and read sensors
probe an input device's analog levels and digital signals
No access to Fab Lab where equipment is located
No text
I wanted to start this assignment of using Micro:bit extension called "inventor's kit" in order to test-drive learning materials I have got to my classroom in the Faculty of Education. I did choose exercise where RGB led is controlled by R, G and B buttons. That LED is a special LED that containts three separate LEDs in one package. The lights from these LEDs can mixed together to allow for the creation of many colours
This assigment was fairly straightforward because I didn't want remix it at all. I just did build the circuitry by following tutorial provided by Kitronik and downloaded code by Kitronik. Same goes with the code, which use Micro:bit PWM outputs to take control over the colours of the LED
So my own contribution wasn't something tangible, more or less it was related on learning how kitronik inventor's kit can be used to teach princiliples of the electronics etc.
Each switch (see breadboard layout figure below) controls the brigthness of one of the colours (SW1: green; SW2: red; SW3: blue). Pressing a switch will increase the brightness of that switch's colour until cycle will go 0 and colour will turn off.
This circuit uses the three analog PWM (pulse-width-modulation) output pins (pins PO, P1 and P2, see the right figure) from the Micro:bit, on of three colors (separate LEDs) inside RGB led.
When one of the three switches is being pressed, one of the pins (with same color) will output a PWM signal (see pink, green and blue jumper cables in breadboard layout diagram). If switch is pressed longer period, it will raise the analog out value in increments of 10, up to maximum of 1010. If the maximum value is exceeded, then the value is reset back to 0.
The switches has pull-down resistors, which hold S1, S2 and S3 (see circuit diagram picture) low until swithc is pressend at which point current will flow from +3v pin through the switch and made it high
The BBC micro:bit is a small computing device for children. One of the languages it understands is the popular Python programming language. The version of Python that runs on the BBC micro:bit is called MicroPython. See more: https://microbit-micropython.readthedocs.io/en/latest/
Microsoft’s MakeCode editor is the perfect way to start programming and get creating with the BBC micro:bit. The colour-coded blocks are familiar to anyone who’s previously used Scratch, and yet powerful enough to access all the features of this tiny computer. You can also switch to JavaScript to see the text-based code behind the blocks. See more: https://makecode.microbit.org/reference
Below is code for this exercise which has been done by using graphical Makecode interface. Makecode is suitable for my lessons in the faculty of education where I teach computer sciences and educational technology to teacher education students
Codebase is running in the forever loop, where three swiches are listened and if one of switches gets value =1 when green/red/blue variable(s) are below 1020, code will add value +10 to variable(s). When it will achieve value 1020, then variable(s) wil be set to value 0
Code is pretty simple even for beginners
Next example is machine generated javascript code, which can be revealed when coding with makecode. When you flip "blocks-javacript" button, graphical coding blocks will be replaced with javascript code. Code is very simple, so it can be used as an example to describe how concepts of computational thinking are present in the code (algorithms, structures etc) but also as an example how to use Microbit I/O pins.
let Red = 0
let Green = 0
let Blue = 0
basic.forever(function () {
pins.analogWritePin(AnalogPin.P0, Red)
pins.analogWritePin(AnalogPin.P1, Green)
pins.analogWritePin(AnalogPin.P2, Blue)
if (pins.digitalReadPin(DigitalPin.P8) == 1 && Green < 1020) {
Green = Green + 10
} else if (pins.digitalReadPin(DigitalPin.P8) == 1 && Green == 1020) {
Green = 0
}
if (pins.digitalReadPin(DigitalPin.P12) == 1 && Red < 1020) {
Red = Red + 10
} else if (pins.digitalReadPin(DigitalPin.P12) == 1 && Red == 1020) {
Red = 0
}
if (pins.digitalReadPin(DigitalPin.P16) == 1 && Blue < 1020) {
Blue = Blue + 10
} else if (pins.digitalReadPin(DigitalPin.P16) == 1 && Blue == 1020) {
Blue = 0
}
})
In this exercise, I will show how to check the potentiometer values (resistance) by NodeMCU development board (ESP8266) and then show value by using intergrated webserver to visitors
This input exercise is pretty simple what comes to electronics, because only NodeMCU and one potentiometer is required for this task (and necessary cables and breaboard of course)
Hardware
Software
Now next step is to configure ESP8266 to run as an standalone www-server and measure analog input (potentiometer) value to be shown on simple webpage
In order to be able to program ESP8266 you need to install Arduino IDE and then install ESP8266 add-on to it. I have explained those pages in the embedded programming week, so please visit here: week 9. Embedded programming.html
In this case you doesn't need to install any other additional libraries, so we can proceed to create actual sketch!
Now, I am going to explain how to configure ESP8266 into Station (STA) mode, and create a web server to serve up web pages to any connected client under existing network
First step is to check what are details of your wifi network. Before uploading the sketch, you need to modify the following two variables witrh your network credentials, so that ESP8266 can establish a connection with existing network
const char* ssid = "YourNetworkName"; // Enter SSID here
const char* password = "YourPassword"; //Enter Password here
In the following code sketch you will see different segments of the code commented, which will help you to understand how this simple potentiometer www-server will work in the practise
/* Simple code, which will measure potentiometer value and
* show it on internal ESP8266 webserver
*/
// include ESP8266 www-server library, which do turn ESP8266 into www-server
#include <ESP8266WebServer.h>
// Analog input A0 is defined to be potentiometer
#define Potentiometer A0
// let's define variable potvalue where potentiometer value is stored
float potvalue;
// wifi settings (these variables need to be adjusted to fit into wifi network )
const char* ssid = "ubinotko1";
const char* password = "";
// www-server will be set up to answer in port 80 and can be called as server in the code
ESP8266WebServer server(80);
/*
* Following codes are run once; this is the spot for setup and configuration
* - In this program this section is used to start www-server and serial-connection (for debugging)
*
*/
void setup() {
// serial connection is openeded
Serial.begin(115200);
delay(100);
// this code puts SSID name to serial console
Serial.println("Connecting to ");
Serial.println(ssid);
//connect to your local wi-fi network
WiFi.begin(ssid, password);
/*check wi-fi is connected to wi-fi network.
* If connection is not established "." will be displayed until connection is ok
*/
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
// When wifi connection is established, serial console will show following messsage
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
// if page request is ok, show handle_onconnect, if not found, then show handle_not_found
server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);
// start server and show it also via serial connection
server.begin();
Serial.println("HTTP server started");
}
/* Put your main code here, to run repeatedly:
* In the context of code this section is used only to listen port 80 (wait potential visitors)
*
*/
void loop() {
server.handleClient();
}
// When page request is done by visitor(s) this section will read potentiometer and send it to www-pages
void handle_OnConnect() {
potvalue=analogRead(Potentiometer);
server.send(200, "text/html", SendHTML(potvalue));
}l
// if requested page is not available, then show error 404 message
void handle_NotFound(){
server.send(404, "text/plain", "Page Not found. I am sorry :(");
}
// This is full html page which will be shown to visitor
String SendHTML(float potvalue){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head>\n";
ptr +="<title>ESP8266 Potentiometer value (resistance) www-server ;-)</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<div id=\"webpage\">\n";
ptr +="<h1>ESP8266 Potentiometer Value Station\n";
ptr +="<p>Resistance: ";
ptr +=potvalue;
ptr +="Ohm</p>";
ptr +="</div>\n";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
In this "input devices" assigment I wanted 1) to test Kitronik Inventor Kit (electronics extension to Microbit) and makecode programming with Micro:Bit and 2) to explore how to connect ESP8266 via wifi and show values in www-server
ESP8266 is important device for me, because I will use newer version if it in the context of my final project. So in my electronics explorations I will use ESP8266 as much as possible.
I was surprised how easy it was to measure analogical input of ESP8266 and turn it into www-page
I will continue with ESP8266. Following explorations will be done in the context of the Fab Academy or outside of it:
In the context of my Final project I will use successor of ESP8266: ESP32 for following purposes