Finding how to task my python skills with hardware has not been easy, because I had only microcontrollers. The issue with using a microcontroller to run python is its limited memory to run python, the only viable option was to get a raspberry pi, but the cost of getting a computer like raspberry pi was way out of my league, but luckily for me, I heard a guy named Damien George created a new version of python that can run on very little memory and is great for microcontrollers.
Why Micropython?
- Easy to learn: Python,itself is an easy language to learn, so micropython will not be an exception. Compared to the conventional c++ used for microcontrollers, micropython is easier to write. For example to toggle an led in C++ Arduino ,the code should look something like this
const int LED_PIN=2;const int BUTTON_PIN;int ledState=HIGH;void setup(){pinMode(LED_PIN, OUTPUT);pinMode(BUTTON_PIN,INPUT_PULLUP);digitalWrite(LED_PIN,LOW);}void loop(){ledState=digitalRead(BUTTON_PIN);if(digitalRead(BUTTON_PIN)==1){// if button was presseddigitalWrite(LED_PIN,!ledState);delay(500); // wait for half a sec}}const int LED_PIN=2; const int BUTTON_PIN; int ledState=HIGH; void setup(){ pinMode(LED_PIN, OUTPUT); pinMode(BUTTON_PIN,INPUT_PULLUP); digitalWrite(LED_PIN,LOW); } void loop(){ ledState=digitalRead(BUTTON_PIN); if(digitalRead(BUTTON_PIN)==1){ // if button was pressed digitalWrite(LED_PIN,!ledState); delay(500); // wait for half a sec } }const int LED_PIN=2; const int BUTTON_PIN; int ledState=HIGH; void setup(){ pinMode(LED_PIN, OUTPUT); pinMode(BUTTON_PIN,INPUT_PULLUP); digitalWrite(LED_PIN,LOW); } void loop(){ ledState=digitalRead(BUTTON_PIN); if(digitalRead(BUTTON_PIN)==1){ // if button was pressed digitalWrite(LED_PIN,!ledState); delay(500); // wait for half a sec } }
Enter fullscreen mode Exit fullscreen mode
But for micropython it is a simple as this
// this example is for a pyboardimport pybbutton = pyb.Switch()while true:sw.callback(lambda:pyb.LED(1).toggle())// this example is for a pyboard import pyb button = pyb.Switch() while true: sw.callback(lambda:pyb.LED(1).toggle())// this example is for a pyboard import pyb button = pyb.Switch() while true: sw.callback(lambda:pyb.LED(1).toggle())
Enter fullscreen mode Exit fullscreen mode
Yes it that simple
Also when it comes to quoting long strings micro python is way easier.
let assume this is our text
hjdbkhfknbjkHBKDVDdkdbkbdkbkjkjBJBKDBjkbdkhdbKDBJDBJKFBJDBkjbdkbfjkbJKBJKBbkbfkBFJKBDJBJdbbdjbnLUkegkdbfkbKD,N kdkdkjd KHDHDVNM hhJHbkeBK VDJKvj dljkdbjbbJbhdkavKBFKAdhbkjbfuhfKJBFJBfkBJKDBKbkjkbjBHBDKdhkndjagdfkjadhdbfkhjbdkhfvhvakhbidghfbkbadbjbafhjbvhvhfbkhabahdgyetkfrhfiabfhfkjksbkjf
To quote this in C++ i will have to quote each line, like this
"hjdbkhfknbjkHBKDVDdkdbkbdkbkjkjBJBKDBjkbdkhdbKDB""JDBJKFBJDBkjbdkbfjkbJKBJKBbkbfkBFJKBDJBJdbbdjbnL""UkegkdbfkbKD,N kdkdkjd KHDHDVNM hJH bkBK VDJKvj""dljkdbjbbJbhdkavKBFKAdhbkjbfuhfKJBFJBfkBJKDBKbkj""kbjBHBDKdhkndjagdfkjadhdbfkhjbdkhfvhvakhbidghfbk""badbjbafhjbvhvhfbkhabahdgyetkfrhfiabfhfkjksbkjf""hjdbkhfknbjkHBKDVDdkdbkbdkbkjkjBJBKDBjkbdkhdbKDB" "JDBJKFBJDBkjbdkbfjkbJKBJKBbkbfkBFJKBDJBJdbbdjbnL" "UkegkdbfkbKD,N kdkdkjd KHDHDVNM hJH bkBK VDJKvj" "dljkdbjbbJbhdkavKBFKAdhbkjbfuhfKJBFJBfkBJKDBKbkj" "kbjBHBDKdhkndjagdfkjadhdbfkhjbdkhfvhvakhbidghfbk" "badbjbafhjbvhvhfbkhabahdgyetkfrhfiabfhfkjksbkjf""hjdbkhfknbjkHBKDVDdkdbkbdkbkjkjBJBKDBjkbdkhdbKDB" "JDBJKFBJDBkjbdkbfjkbJKBJKBbkbfkBFJKBDJBJdbbdjbnL" "UkegkdbfkbKD,N kdkdkjd KHDHDVNM hJH bkBK VDJKvj" "dljkdbjbbJbhdkavKBFKAdhbkjbfuhfKJBFJBfkBJKDBKbkj" "kbjBHBDKdhkndjagdfkjadhdbfkhjbdkhfvhvakhbidghfbk" "badbjbafhjbvhvhfbkhabahdgyetkfrhfiabfhfkjksbkjf"
Enter fullscreen mode Exit fullscreen mode
But for micropython i just have to “triple quote”
"""hjdbkhfknbjkHBKDVDdkdbkbdkbkjkjBJBKDBjkbdkhdbKDBJDBJKFBJDBkjbdkbfjkbJKBJKBbkbfkBFJKBDJBJdbbdjbnLUkegkdbfkbKD,N kdkdkjd KHDHDVNM hhJHbkeBK VDJKvj dljkdbjbbJbhdkavKBFKAdhbkjbfuhfKJBFJBfkBJKDBKbkjkb jBHBDKdhkndjagdfkjadhdbfkhjbdkhfvhvakhbidghfbkbadbjbafhjbvhvhfbkhabahdgyetkfrhfiabfhfkjksbkjf""""""hjdbkhfknbjkHBKDVDdkdbkbdkbkjkjBJBKDBjkbdkhdbKD BJDBJKFBJDBkjbdkbfjkbJKBJKBbkbfkBFJKBDJBJdbbdjbnLU kegkdbfkbKD,N kdkdkjd KHDHDVNM hhJHbkeBK VDJKvj dljkdbjbbJbhdkavKBFKAdhbkjbfuhfKJBFJBfkBJKDBKbkjkb jBHBDKdhkndjagdfkjadhdbfkhjbdkhfvhvakhbidghfbkbadb jbafhjbvhvhfbkhabahdgyetkfrhfiabfhfkjksbkjf""""""hjdbkhfknbjkHBKDVDdkdbkbdkbkjkjBJBKDBjkbdkhdbKD BJDBJKFBJDBkjbdkbfjkbJKBJKBbkbfkBFJKBDJBJdbbdjbnLU kegkdbfkbKD,N kdkdkjd KHDHDVNM hhJHbkeBK VDJKvj dljkdbjbbJbhdkavKBFKAdhbkjbfuhfKJBFJBfkBJKDBKbkjkb jBHBDKdhkndjagdfkjadhdbfkhjbdkhfvhvakhbidghfbkbadb jbafhjbvhvhfbkhabahdgyetkfrhfiabfhfkjksbkjf"""
Enter fullscreen mode Exit fullscreen mode
if you are interested in learning micropython, you can visit micropython.org there are documentations for the most common micro python boards. The most common and cheapest boards that can handle micropython in Nigeria are the ESP8266 12E and ESP32 boards.
To learn more about ESP8266 with micro-python click here
To learn more about ESP32 with micro-python click here
Note: Although micro-python is very easy to write, I would not recommend in for beginners because it is still a new language and doesn’t have a lot of resources to help beginners unlike arduino that has been around for a long time, beginner friendly and has unlimited resources to help both pro’s and beginners. In arduino there is a library for almost anything you can think of. so for newbies in the embedded system world I would advice you start with arduino.
If you are a Nigerian and you are interested with learning micropython you can buy the ESP8266 board here
or the ESP32 here
Thanks for reading.
暂无评论内容