Pages

Wednesday, March 15, 2017

Micro:bit introduction


While surfing the net looking at my favorite technology sites I stumbled upon a small microcontroller board called the Micro:bit. I never really dug into that as it was not easily onbtainable in the Netherlands. And then suddenly my favorite (non-china based) reseller Kiwi-Electronics (I am not related to them in any way) was stocking them. So it drew my attention and I decided to buy one of these.

A bit of background.

In England the Micro:Bit was free of charge distributed amongst all 7th graders in schools all over the UK. It was a initiative of the BBC and some large names are involved like Microsoft, The British Council, Lancaster university etc. etc. The purpose is (like many years ago with the legendary BBC computer) to get kids coding.

Because the impact of distributing the Micro:bit for free amongst kids (1.000.000 pieces were given away) is so big there has to be a lot of assistance and there is.

There are several programming languages available amongst whom are Python, Blocks, Java, Microsofts Touch, Fuze Basic and Forth. ARM Mbed even has an on-line IDE for C/C++ development available at http://developer.mbed.org.
You can find many examples, projects and interactive programming on the official website: http://microbit.org

So what's the fuzz all about.




Well first Micro:bit has a great programming environment. At the microbit.org website just choose Let's code and you can choose from several languages which all have a web-ide. This makes programming a piece of cake. Many languages even have an onboard web-simulator so you can test your code before transferring tit (through USB) to the Micro:bit itself.

Next to that you can program it from an Android device by pairing it with it's build in Bluetooth. And naturally you can program it from my second favorite devellopment machine: the Raspberry Pi.

The hardware.

Besides having a great software backup the hardware is impressive.


The Micro:bit can use a USB connection (think powerbank) or batteries as a power source.
There are two buttons available for interaction with programs.
It has an array of 5 x 5 leds on which you can scroll texts or have simple graphics. The individual leds brightness can be controlled.

It's got 3 large I/O connections to be used as a digital input or output but also as an analog input.
And onboard there is a compass, thermometer and an accelerometer.
And last but not least the Micro:bit has build in Bluetooth LE so it can be paired with your smart-phone, tablet, Raspberry Pi or even another Micro:bit.

The first project.

So when I first laid my hands on a Micro:bit I started with scrolling some text on its array of leds. I downloaded a few examples and played a bit with it getting familiar with the programming environment.

Next step was a small project to test the analog input and the capability of controlling a servo. And that is what I am going to demonstrate here.

To test the analog input I used a flex sensor. Flex sensors are resistors who's value alter when you bend them. A variation on this are the pressure sensors who's resistance vary when you put pressure on them. Both kinds are rather expenisive. So you can substitute the flex-sensor by a regular potmeter.

However you can also make your own flex-sensors for next to nothing with some velostat. You can read how to build your own flex-sensors in an upcoming story. So stay tuned.

The hardware setup.



As you can see in the schematic above the setup is simple.
The flex sensor is connected with a pull-up resistor to pin 0 of the Micro:bit and the servo is connected to pin 1
I powered the servo and the Micro:bit seperately as I was afraid that the Micro:bit was not able to deliver sufficient power. In the end I was wrong. The Micro:bit is capable of powering the servo by itself, however that is pushing the limit of its onboard power regulator.



The picture above shows you my real-life setup.
At the bottom on the right side connected with the alligator clips is my home made flex-sensor in strecthed shape.

Reading analog

For programming I chose Microsoft PXT which is a block-based editor (like Scratch or App-Inventor) which translates the blocks into Javascript.


So the blocks shown here read the analog pin, then show the read number on the led matrix and pause 500 miliseconds before taking another reading. The Forever blocks makes thism program run infinit.


And this is the Javascript code it automatically translates into.

Adding the servo

Next step was to translate the reading of the flex sensor into a servo movement. This means that the more you bend the flex-sensor the farther the arm of the servo will turn.


The block code is really simple again.
I read the analog input and limit it on the upperside to 100 and on the lower side to 40.
The difference between the minimum of 40 and maximum of 100 gives me a range of 60 which can easily be translated into servo steps. A servo can move in 180 degrees. So each of my 60 steps represent 3 degrees of the servo.
So the program displays the angle on the led matrix and puts the servo in that position.
Done in essentially 13 lines of code.


And here is the translated Javascript code.

Fun

The Micro:bit is fun to play with. The PXT block editor is easy to use and gives results very fast. And the results can be tested by the build in emulator which makes it even easier to test your projects. I surely can see that this is a great tool to get children interested in programming and electronics. And for adults it is a fast way to learn the basics of Python and get immediate results.

Limitations and expansion


For what it is intended to do the Micro:bit is really great. However when you need to do some real world projects the 3 standard I/O ports are a bit limited and as the Micro:bit has no wifi it is really restircted in its communication.


For the adventurous amongst us there is a break-out board with headers. So attaching the Micro:bit with Dupont wires to a breadboard is possible and you will get a load of I/O ports on the way.


The picture above shows you all the available I/O ports when using the break-out board. You can find a full description of the pins on the Micro:bit website in this link https://www.microbit.co.uk/device/pins

These I/O ports will make the Micro:bit much more usable for projects. However it will then be more competing with the Arduino series of boards as there is still the lack of wifi communication.

The competitions answer.

In the educational world there is a big competitor for the Micro:bit being the Raspberry. Well the Raspberry is much more advanced as it is a complete linux machine with HDMI output to a TV screen or monitor. However the big barrier for the Raspberry for kids is its price-tag. The Raspberry Pi 3 costs about 39 euro which is a lot of money for kids. Do not get me wrong, the Raspberry foundation does a tremendous job in teaching kids programming with the Raspberry but loads of projects made with the Raspberry are real-world projects because it is a full fledged computer and a fast one to !!!



In comes the Raspberry Pi Zero. This will be a game-changer as it is a full Raspberry Pi (be it a slower version, it is a bit faster as the Pi 1) and has the incredible price-tag of 5.50 Euro. That's right a full fledged linux machine with USB and HDMI for less as 6 euro. And just released is the Pi Zero W which has on board Wifi and Blue-Tooth for just 11 Euro. Now that is amazing !!
This is a price-tag which is affordable for kids but also makes it a real competitor for Arduino and ESP projects.


Let's compare the prices of both; Remember that the Micro:bit was free distributed amongst kids in the Uk but if you want to buy one extra for your products it is at the moment that I am writing this about 14 Euro. So the Raspberry Pi Zero is about at one third of that price and even the Pi Zero W with Wifi and Bluetooth is cheaper.

There will be more stories coming up on the Micro:bit and on the amazing Raspberry Pi Zero. So watch this space.

Till then, have fun.

Luc Volders

Wednesday, March 1, 2017

Back to Basic - Basic language on the ESP8266


When I was a young guy of about 17 years old (we are talking 1976) I bought my first computer which was basically a bag of chips with a calculator keyboard and a calculator-like display. It was based on a RCA 1802 microprocessor and could only be programmed in machine language (thats even lower as assembly).



Later I moved on to the Commodore PET which was the predecessor of the Commodore 64. Just look at that beauty. At that time computers still looked like computers !! The Pet (personal Electronic Transactor) could be programmed in Basic which was a huge leap forward. I did marvels in Basic. I wrote a simulator for an assembly language that was ported to a Burroughs Mainframe and was used at the HTS in Eindhoven for study purposes. I wrote a database program (Elektronische Kaartenbak), a spreadsheet program (Volderscalc) and a complete accounting package (VOLAD) in Basic and all this on my faithfull PET with only 32K of memory.

And now in the age of Arduino and ESP-8266 I had to learn a whole bunch of new languages: C++, Lua, Python and HTML.

Basic as a language is nowadays looked upon as inferior by many programmers. The reason for this is that Basic has Goto statements. This makes it possible to jump from one part of the program to another part at any instance. To many programmers this kills structured programming and makes it possible to code like spaghetti.

So I was totally pleased when suddenly there was a Basic for the ESP-8266. And if I may say so not an ordinary Basic but a real splendid one. ESP8266-Basic has so many features build in that it is possible to read a thermometer sensor and display the values on a webpage in just 2 lines of code: amazing.

So lets do it: lets program in Basic on the famous ESP8266.

Installing ESP8266-Basic.

Before we can start programming in Basic we need to install the language on the ESP-8266 itself. Well that's a piece of cake. First download the ESP Flasher from this page:
https://www.esp8266basic.com/download.html

Put the Flasher in a sub-directory on your computer and start it.
The Flasher indicates the Com-Port on which it found the ESP8266 and you will have to tell it the amount of memory the ESP8266 has. For the ESP8266-01 choose 512K or 1M. For the NodeMCU and its varieties types use 4M.


Next choose Flash Firmware and the led on the ESP8266 will start to blink.





On screen you will see the flashing progres and after a while flashing is ready and the info on your screen will look as the picture above.

Wireles Station or stand-alone.

When Basic has been flashed into the ESP we can start using it.
At first ESP8266-Basic is not connected to your router because (off course) it does not know your routers nae and password to get access.

So ESP8266-Basic has to be connected to as an Access Point.

Start Wifi on your phone or tablet and look for the accesspoints. One will be called ESP followed by some number (which is actually the MAC adress of your ESP8266). Activate this accesspoint on your device and make sure it is connected to your phone or tablet.

For properly using ESP-8266 Basic you will need to use Firefox or Chrome as browser on your Android device.


Now start the browser and surf to 192.168.4.1 and you will see the start screen of ESP-8266 Basic.

As you can see I am using ESP8266-Basic 3.0 Alpha 51.
Now I am experiencing a small problem with this version and that is that not all menu items are directly available. So I found a way around this. Just follow the steps below and you're all safe.

First choose the menu item EDIT.

Edit starts with a program called default.bas which is not available in the ESP-8266. So we will make our first program. In the program area just type the first program line:

print "hello"

Next step is to click on Save. and that's it. You wrote your first program. Which is at the same time the workaround. All Menu items are available now.


Now if you click on Run Basic will run the program which just prints the word hello on your screen.

That's it.

The other menu functions

As you have seen there are several menu items available.

VARS
will give you a list of used variables and all the ESP pin states !!

EDIT
is used to edit programs like we just did above

RUN
will start the last Edited and saved program

DEBUG
is used to help you debug any faults in your program with a pause function, showing you which line is executed and at the same time showing a list of variables.






FILE MANAGER
is just what it suggests. You can browse your computer or Android device for files and upload them to the ESP. And you can choose in the file list the program you want to Edit, Delete or Rename. When choosing a program and choosing Edit you will be directed to the Edit screen.
The Filemanager menu was only available after I wrote and saved my default.bas program





SETTINGS
Settings is an important item. In settings you can put the name of your Access point and Password so you can access ESP8266-Basic from any device (computer, laptop or Android device) in your network. As I find my phones screen to small for daily programming I program ESP8266-Basic from my computer.
The Settings menu was only accessible after I wrote and saved my default.bas program.


Another important setting is wether ESP-Basic has to start the default.bas program directly at starting up. With this setting your program will automatically run each time the ESP is powered up.

Access from within your network.




After filling in your Accesspoints name and password in the SETTINGS page ESP-Basic will come up in your router as a new device.



Look in your router for the IP adress for the ESP and start a new window with the IP-adress as URL. So now we are ready to go.

Now why would I program the ESP from my desktop machine. Well it makes it easy to open multiple windows, search for Basic programs copy them and paste them in the ESP8266-BASIC EDIT window. Makes programming very easy.

Dallas 18b20




The Dallas 18b20 is the ideal thermometer. It has a wide range of measurement from -55 degress Celsius to +125 degrees Celsius. Its accuracy is 0.5 degrees Celsius and it is waterproof. So theoretically you can put it in aboiling kettle and measure the temperature. I have seen tinkerers putting one in a freezer to check the temperature. As it is waterproof you can use it for measuring outside temperature.

It uses a specific communiaction protocol and communicates over just 1 wire. There is a lot to tell about this wonder-thermometer but I'll save that for a rainy day.

Most important is that it easily communicates with ESP8266-Basic.

Measuring temperature with the Dallas 18b20

There is a ton on information about the Dallas 18b20 on the internet. And we do not need it at all for using it with ESP8266-Basic. You just have to remember one thing and that is that the Dallas 18b20 dataline NEEDS to be attached to D4 (GPIO2) on the NodeMCU board.


[EDIT] Oops !!! Just saw a mistake on the breadboard layout down here. I forgot to attach the 5 volt lead from the NodeMCU to the breadboard.So put a small lead from Vin (bottom right) to the VCC line.




Now that is done we need to write our Basic program. And here comes the neat part. We only need two lines of code:



So let us look at the program:

temnow = temp(0)
wprint temnow


First we read the Dallas thermometer with the command temp(0) and put it in the variable temnow
the second line puts the variable on the screen.

That's it.



The above picture is the result when we run this program.
The temperature with an accuracy of 4 decimals in just 2 lines of code.

Now this accuracy is fine but I want just 1 decimal for better readaility. Well here we go:



temnow = temp(0)
temnow = int(temnow*10)/10
wprint temnow


Save the program and run it.




The second line in our program multiplies the temnow variable with 10 and makes an integer from it. Next it is divided by 10 again and there we have our value with just 1 decimal.

A nice looking webpage

Now look at the next picture of a webpage.



It is the same temperature reading but now in a nice web-page with a background color, a header for the page info. The temperature reading in a contrasting color and a bar that indicates wether the temperature is lowering or rising. And 2 buttons of which one takes another reading and the other one stops this program.

Man, this must be complicated to program......... NOT

 memclear  
 cls  
 temnow = temp(0)  
 temnow = int(temnow * 10)  
 temnow = temnow / 10  
 wprint "<!DOCTYPE html>"   
 wprint "<html> <body>"  
 wprint |<body style="background-color:powderblue;">|  
 wprint |<H1><span style="color: red;">|  
 wprint "Thermometer"  
 wprint "</H1>"  
 wprint "</span>"  
 wprint |<H2><span style="color: blue;">|  
 wprint "Temperature = "  
 wprint temnow  
 wprint "</span>"  
 wprint "</H2>"  
 meter temnow, 0, 40  
 wprint "<br>"  
 wprint "<br>"  
 button "Again", [again]  
 wprint "............"  
 button "Stop", [quit]  
 timer 1000, [refresh]  
 wait  
 [refresh]  
 temnow = temp(0)  
 Wait  
 [again]  
 timer 0  
 run  
 wait  
 [quit]  
 end  


Just look at this source code. Most of it is HTML commands for formatting the page layout.
Putting a button on your web-page is done by the command:

button "Again", [again]

and the program just jumps to the label [again] if you press that button.

I think you can figure out the rest of the program for yourselves as it is dead-easy.

Neopixels

Remember the Neopixels. Those charming RGB leds for which you need just 1 dataline to control hundreds of them. Well programming them in ESP8266-Basic is a piece of cake.

But before you start using the Neopixels with ESP8266-Basic I urge you to read the story I wrote about the neopixels. You can switch to that story in a new tab by clicking here.
http://lucstechblog.blogspot.nl/2015/10/neopixels-ws2812-intro.html

 



First set up your breadboard like the above picture and connect the Neopixel data line to the NodeMCU D8 line. ESP8266-Basic uses this pin as its default pin for accessing the neopixels. You can alter this with the neo.setup() command. If you want to connect the neopixels to D7 for example you start the ptogram with the next line:

neo.setup(d7)

In the example below I used 7 Neopixels.

Now type in the editor the next program and save it:




neo.cls()

for x=0 to 6
neo(x,0,0,255,0)
delay 1000
next x

The Neopixel command neo() uses the following syntax:
the first parameter (x in this case) is the number of the neopixel in the chain starting at 0
The next 3 figures (0,0,255) represent the RGB code which is in this case blue
The last figure 0 in this case tells Basic to immediately change the color.

At first the Neopixels will be off. Then one by one they will light up in blue until they all display blue and the program stops.

Anybody who is familiar with programming will appreciate how easy it is to do amazing things with ESP-Basic.

IFTTT

A lot of automation can be done with IFTTT (If This Then That). IFTTT is much used by ESP-8266 enthousiasts as it easily can be used to trigger all kinds of events for IOT (Internet of Things) projects. One of the upcoming stories is how to use IFTTT to send a Twitter message if it rains using my rain-detector described in the story you can read by clicking here.

Adressing IFTTT can be a real pain in some of the programming languages.

In ESP8266-Basic it is just a few lines of code:

iftttkey = "XXXYYYZZZXXXAA"
trig = "maker.ifttt.com/trigger/givenotice/with/key/" & iftttkey
print wget(trig)

And that really is all !!!!



Making an API call has never been easier in any programming language (as far as I know).


Final Words

ESP8266-Basic is really easy to work with and well documented.


Essentially it is Basic with loads of built in libraries. This makes sure that we can focus on the actual programming and not on putting all kinds of code together for just getting some sensors to work.....

The above shown examples are just a very small part of what ESP8266-Basic is capable off. I haven't discussed Telnet, Serial communications, Timers, File I/O, using the ESP I/O pins, Oled-LCD and TFT screen drivers, web-interface commands, email, web-graphics, mathematic and string functions, i2c, SPI, UDP and even Infrared control.

I sincerely thank the maker MMISCOOL for bringing such a great product to the ESP8266. And I am certainly going to use ESP8266-Basic in my future projects.

You can find the official ESP8266-Basic website here: https://www.esp8266basic.com/
The official webpage also contains examples and the complete manual :  https://www.esp8266basic.com/language-reference.html
And you can find a ton of information, projects and examples  on the ESP8266-Basic Forum: http://www.esp8266.com/viewforum.php?f=38

So till next time
Have fun

Luc Volders