JavaScript on Microcontrollers

A presentation at HalfStack Phoenix in January 2020 in Phoenix, AZ, USA by Niels Leenheer

Slide 1

Slide 1

JavaScript on Microcontrollers

Slide 2

Slide 2

Hi, I’m Niels. I am so happy to be here. Not my first time speaking in a movie theatre, but I love this. Especially that you can just order stuff. Love that. This is actually the sixth HalfStack I’ve spoken at. And this is my first time in Phoenix! This is so different from what I’m used to. Love the Cactusses.. Cactii. Cactuses?

Slide 3

Slide 3

I’m Dutch. I’m from the Netherlands. Some people also call it Holland. Those people would be wrong…. First, there are some myths about Dutch people. No, we don’t all have wooden shoes. I literally know no-one that owns a pair of wooden shoes.

Slide 4

Slide 4

And it’s kind of like open carry. Yes, it is technically legal to bring your AK-47 to the supermarket - just like you can wear wooden shoes in public, but there will be people that think you are an idiot.

Slide 5

Slide 5

You’ve definitely heard of the capital Amsterdam. Which is known for it’s Cana…. Canals. Yeah.

Slide 6

Slide 6

Quite often when I fly out to a conference from Amsterdam which is almost a guarantee that there will be a sniffer dogs waiting when you arrive at your destination to see if some you brought some of that canals with you. Yeah, lots of fun.

Slide 7

Slide 7

Anyway, I am a Mozilla Tech Speaker, Google Developer Expert.

Slide 8

Slide 8

I tweet at html5test.

Slide 9

Slide 9

I love the web. I’ve been a web developer since 1994. I written my first HTML 25 years ago. Yes I am old. I love HTML, I love CSS and I even love JavaScript. Okay, grown to love it.

Slide 10

Slide 10

And I love electronics. When I was a teenager I had this cardboard electronics set with all kinds of different components…

Slide 11

Slide 11

And you could connect them together with clipping wires between some springs. You could build a radio or even a transmit morse code.

Slide 12

Slide 12

And this is what I have now. Still some components to connect together with some wires. But the components have become much more advanced. Instead of transistors and diodes and resistors, we now play with microcontrollers and all kinds of sensors. And everything works together.

Slide 13

Slide 13

I know some of you are thinking, why should I care about this? I am a web developer, I don’t have anything to do with electronics. And you’re right.

Slide 14

Slide 14

But when you connect devices to the web you get the Internet of Things. You get devices that talk to servers, and even directly to browsers. And there things start to get interesting for web developers.

Slide 15

Slide 15

The main issue I’ve seen when web developers start playing with microcontrollers and IoT is the barrier of having to lean C. Now C and Javascript may look quite similar syntacticly, but is a completely different beast.

Slide 16

Slide 16

You not only need to learn C, but also low level programming concepts like memory management and pointers. Even though I learned this back in school 25 years ago, – I am a dinosaur – I hate this.

Slide 17

Slide 17

What if we could just use a language that we already know? What if we did not need to worry about those low-level concepts?

Slide 18

Slide 18

What if we could JavaScript on Microcontrollers? And not only that, what if we could leverage the browser to connect to these microcontrollers and let them extend each others capabilities.

Slide 19

Slide 19

This is all fine and well, but…..

Slide 20

Slide 20

Microcontrollers are slow. They are really slow. And when I say slow, that really does not convey the idea how slow they actually are. They are slooooooooow. Take for example the slowest computer you can buy today. The Raspberry Pi.

Slide 21

Slide 21

This is the Zero W, which is slow. Raspberry Pi’s are all slow, but this is the slowest they make. It is still a great little computer. It runs Linux, can use Node.js and even run a full browser like Chrome or Firefox. But this is a computer. It is not a microcontroller.

Slide 22

Slide 22

This is a microbit. It is a little board created for schoolchildren. And it runs at just 16 MHz and has only 16 KB of RAM. It is slow. But it is not even by far the slowest microcontroller you can find. But it is fast enough to run JavaScript.

Slide 23

Slide 23

Now comparing it the Raspberry Pi. That is 60 times faster than a Microbit.

Slide 24

Slide 24

And it has way more memory. So the microbit is on a completely different level.

Slide 25

Slide 25

Are you crazy? Two years ago when I started playing with microcontrollers I would have said yes. But there are very good reasons why running JavaScript on microcontrollers is brilliant.

Slide 26

Slide 26

Microcontrollers are very power efficient. I can run JavaScript on a tiny little Bluetooth enabled board and power it using a coin cell battery. And it can run for months. Slow is a good thing, because when we make things faster, they usually require more power.

Slide 27

Slide 27

And microcontrollers are fast enough. Yes they are slow, but we only have to solve very small problems. Microcontrollers are fast enough for everything I am going to show today.

Slide 28

Slide 28

And finally they do not need an operating system. At least not in the traditional sense. That may sound bad, but is actually fantastic. Because when I flick the powerswitch, I do not want my Linux powered lamp to take…

Slide 29

Slide 29

…45 seconds to boot before the lights turn on.

Slide 30

Slide 30

So let’s take a look at how we can get JavaScript on our microcontrollers. There are lots of different projects, in various stages of maturity and each supporting different types of microcontrollers.

Slide 31

Slide 31

But we’re going to focus on…. Espruino. Is an open source project. But you can also buy hardware with Espruino already installed. So it is plug and play. There are also has versions that you can flash yourself on your own hardware.

Slide 32

Slide 32

And it is an interpreter that supports most of JavaScript, even some ES6 features like arrow functions and so. And it even have REPL functionality. So connect to your computer using a USB cable or using Bluetooth and you can execute commands on the microcontroller just like you would in the console of a browser.

Slide 33

Slide 33

Time for the demo’s. The fun part of this talk. I hope…

Slide 34

Slide 34

But I do have some warnings to give. This is cutting-edge stuff. Hardware demo’s with experimental technology. And most of it using Bluetooth, a wireless connection…. And Bluetooth uses the same frequency as WiFi, so with a room full of WiFi devices there is lots of opportunity for interference. So this is moment where my heart rate goes through the roof.

Slide 35

Slide 35

I am not kidding. This is me at Halfstack London, 2 years ago where none of my demo’s worked.

Slide 36

Slide 36

#1 Connected lightbulb

Slide 37

Slide 37

Slide 38

Slide 38

Slide 39

Slide 39

#2 Pixelart display

Slide 40

Slide 40

Slide 41

Slide 41

Slide 42

Slide 42

Slide 43

Slide 43

Slide 44

Slide 44

#3 Remote controlled car

Slide 45

Slide 45

Slide 46

Slide 46

Slide 47

Slide 47

Slide 48

Slide 48

Slide 49

Slide 49

Slide 50

Slide 50

Lasers!

Slide 51

Slide 51

Invisible lasers!

Slide 52

Slide 52

Tiny invisible lasers!

Slide 53

Slide 53

Slide 54

Slide 54