|
| 1 | +# Background |
| 2 | + |
| 3 | +## What's a microcontroller? |
| 4 | + |
| 5 | +A microcontroller is a *system* on a chip. Whereas your laptop is made up of |
| 6 | +several discrete components: a processor, RAM sticks, a hard drive, an ethernet |
| 7 | +port, etc.; a microcontroller has all those components built into a single |
| 8 | +"chip" or package. This makes it possible to build systems with minimal part |
| 9 | +count. |
| 10 | + |
| 11 | +## What can you do with a microcontroller? |
| 12 | + |
| 13 | +Lots of things! Microcontrollers are the central part of systems known as |
| 14 | +*embedded* systems. These systems are everywhere but you don't usually notice |
| 15 | +them. These systems control the brakes of your car, wash your clothes, print |
| 16 | +your documents, keep you warm, keep you cool, optimize the fuel consumption of |
| 17 | +your car, etc. |
| 18 | + |
| 19 | +The main trait of these systems is that they operate without user intervention |
| 20 | +even if they expose a user interface like a washing machine does; most of their |
| 21 | +operation is done on their own. |
| 22 | + |
| 23 | +The other common trait of these systems is that they *control* a process. And |
| 24 | +for that these systems usually have one or more sensors and one or more |
| 25 | +actuators. For example, an HVAC system has several sensors, thermometers and |
| 26 | +humidy sensors spread across some area, and several actuators as well, heating |
| 27 | +elements and fans connected to ducts. |
| 28 | + |
| 29 | +## When should I use a microcontroller? |
| 30 | + |
| 31 | +All these application I've mentioned, you can probably implement with a |
| 32 | +Raspberry Pi, a computer that runs Linux. Why should I bother with a |
| 33 | +microcontroller that operates without an OS? Sounds like it would be harder to |
| 34 | +develop a program. |
| 35 | + |
| 36 | +The main reason is cost. A microcontroller is much cheaper than a general |
| 37 | +purpose computer. Not only the microcontroller is cheaper; it also requires much |
| 38 | +less external electrical components to operate. This makes Printed Circuit |
| 39 | +Boards (PCB) smaller and cheaper to design and manufacture. |
| 40 | + |
| 41 | +The other big reason is power consumption. A microcontroller consumes orders of |
| 42 | +magnitude less power than a full blown processor. If your application will |
| 43 | +run on batteries that makes a huge difference. |
| 44 | + |
| 45 | +And last but not least: (hard) "real time" constraints. Some processes require |
| 46 | +their controllers to respond to some events within some time interval (e.g. a |
| 47 | +quadcopter/drone hit by a wind gust). If this "deadline" is not met, the process |
| 48 | +could end in catastrophic failure (e.g. the drone crashes to the ground). A |
| 49 | +general purpose computer running a general purpose OS has many services running |
| 50 | +in the background. This makes it hard to guarantee execution of a program within |
| 51 | +tight time constraints. |
| 52 | + |
| 53 | +## When should I *not* use a microcontroller? |
| 54 | + |
| 55 | +Where heavy computations are involved. To keep their power consumption low, |
| 56 | +microcontrollers have very limited computational resources available to them. |
| 57 | +For example, some microcontrollers don't even have hardware support for floating |
| 58 | +point operations. On those devices, performing a simple addition of single |
| 59 | +precision numbers can take hundreds of CPU cycles. |
| 60 | + |
| 61 | +## Why use Rust and not C? |
| 62 | + |
| 63 | +Hopefully, I don't need to convince you here as you are probably familiar with |
| 64 | +the language differences between Rust and C. One point I do want to bring up is |
| 65 | +package management. C lacks an official, widely accepted package management |
| 66 | +solution whereas Rust has Cargo. This makes development *much* easier. And, IMO, |
| 67 | +easy package management encourages code reuse because libraries can be easily |
| 68 | +integrated into an application which is also a good thing as libraries get more |
| 69 | +"battle testing". |
| 70 | + |
| 71 | +## Why should I not use Rust? |
| 72 | + |
| 73 | +Or why should I prefer C over Rust? |
| 74 | + |
| 75 | +The C ecosystem is way more mature. Off the shelf solution for several problems |
| 76 | +already exist. If you need to control a time sensitive process, you can grab one |
| 77 | +of the existing commercial Real Time Operating Systems (RTOS) out there and |
| 78 | +solve your problem. There are no commercial, production-grade RTOSes in Rust yet |
| 79 | +so you would have to either create one yourself or try one of the ones that are |
| 80 | +in development. |
0 commit comments