Search the Community
Showing results for tags 'micropython'.
-
Microcontrollers are known for its low power usage and limited resources thus often deemed unable to understand Python script as Python need interpretor to translate script into machine langauge and intepretor are usaully quite resource-consuming. However, MicroPython has arisen as a lean and efficiant Python 3 interpretor that can be run on ARM Cortex-M series microcontrollers. Ameba RTL8722 is an ARM Cortex-M33 microcontroller that has dual-band WiFi and BLE 5.0, other than that it is fully capable of running MicroPython and controls WiFi using Python script. In addition, its requirement for developing platform is also quite minimal-- only needs a serial port terminal like Tera term. Here is an example of RTL8722 controlling WiFi using just a few lines of Python code on Tera Term, from wireless import WLAN wifi = WLAN(mode = WLAN.STA) wifi.scan() Right after the last line, you will be able to see the result printed on the Tera term almost immediately, Hope this post give you some ideas of how easy it is to control microcontroller using MicroPython, stay tuned and happy coding!
-
Have you wondered how many WiFi are there in your surroundings? What are they? Any free network I might connect to? Using MicroPython on Realtek Ameba RTL8722DM, you can do it in just 1 line of Python code, here is how, Materials Ameba x 1 Steps WiFi Scan function can help us quickly discover what WiFi networks are available in our surrounding. This example does not require any additional hardware, thus simply copy, and paste the following code into REPL to see its effect. #The long version from wireless import WLAN wifi = WLAN(mode = WLAN.STA) wifi.scan() # The short version WLAN(WLAN.STA).scan()
-
For an IoT development board, the tricky part is connecting to other wireless devices, for example wifi router or bluetooth sensor, now with MicroPython, you can connect to any wifi device with only few line of code typed in real time, no compilation is needed! Check out how ameba RTL8722DM does that as follows, Materials Ameba x 1 Steps Ameba can connect to WiFi access point with open security or WPA2 security type, which is the most common security type used in household wireless routers. Here we are going to connect to a WiFi access point using code below, copy and paste the following code line by line into REPL to see their effects. from wireless import WLAN wifi = WLAN(mode = WLAN.STA) wifi.connect(ssid = "YourWiFiName", pswd = "YourWiFiPassword")
-
Take a look of UART communication with Ameba RTL8722 which runs MicroPython Materials Ameba x 1, TTL USB to Serial module x 1 Steps UART is a very versatile communication protocol and almost an essential part of a microcontroller. A TTL USB to Serial module is an IC that helps to translate UART signal to USB signal so that we can see uart log printed on our PC. This module is often found on many development boards, including ameba. However, the module on Ameba is reserved for LOG UART and Firmware uploading, that is why we need a separate module to communicate between ameba and PC. There are currently 2 sets of UART available to MicroPython users and they are, Unit TX RX 0 PA_21 PA_22 3 PA_26 PA_25 Here we are using unit 0 to demonstrate how uart works on ameba. Connect TTL module to PA_21 and PA_22 as shown below, Then, copy and paste the following code line by line into REPL to see its effect. from machine import UART uart = UART(tx="PA_21", rx= "PA_22") uart.init() uart.write('hello') uart.read(5) # read up to 5 bytes
-
Time is very useful when come to medium size project that requires periodical triggering of certain task over time, here we are going to show you how ot program Ameba RTL8722 dual-band wifi and BLE microcontrollers, Materials Ameba x 1 Steps There are 3 sets of general timers available to user, each at 32KHz, they are timer 1/2/3. Here we use timer 1 as example to demonstrate how a periodical timer works. Copy and paste the first 3 lines of code into REPL to see its effect. from machine import Timer t = Timer(1) # Use Timer 1/2/3 only t.start(2000000, t.PERIODICAL) # Set GTimer fired periodically at duration of 2 seconds, printing text on the terminal # To stop the periodical timer, type t.stop() A text of “–timer triggered. to stop: type t.stop()–” will be printed on the terminal every 2 seconds.To stop the timer, simply type t.stop().
-
SPI is a highly important peripheral for microcontroller as it allows for high speed communication over short range, here is how we use python to send msg to another microcontroller over SPI 材料准备 Ameba x 1, Arduino UNO x 1 范例说明 SPI是一种快速且强大的通讯协议,并通常在微处理器中被用来接受传感器的数据或输出图像讯号。在这个示例中将示范ameba如何透过MicroPython以从属模式接收数据。 在通讯连接建立之前,需要先将以下代码烧录到Arduino UNO。 rtc = RTC() /////////////////////// // SPI Master Write // /////////////////////// #include void setup (void) { Serial.begin(115200); //set baud rate to 115200 for usart digitalWrite(SS, HIGH); // disable Slave Select SPI.begin (); } void loop (void) { char c; digitalWrite(SS, LOW); // enable Slave Select // send test string for (const char * p = "Hello, world!\r" ; c = *p; p++) { SPI.transfer(c); Serial.print(c); } Serial.println(); digitalWrite(SS, HIGH); // disable Slave Select delay(2000); } Arduino UNO将以如下图所示的连接方式和Ameba连接,我们使用第“0”组SPI作为从机, 并将Arduino UNO当作SPI的主机。 然后复制以下代码并粘贴到REPL的粘贴模式窗口,并等待代码生效。 from machine import SPI s1= SPI(0 , mode = SPI.SLAVE) for i in range(14): chr(s1.read())
-
RTC helps to reveal the real time to the machine thus it's critical to some applications. Using MicroPython on Ameba RTL8722, we can get RTC time in just 2 lines, here is how, Materials Ameba x 1 Steps RTC module help microcontroller to keep track of time and is essential to our time module. Here we an example to demonstrate how to get local time and update the time. Copy and paste the following code line by line into REPL to see its effect. rtc = RTC() rtc.datetime() # get date and time rtc.datetime((2020, 12, 31, 4, 23, 58, 59, 0)) # set a specific date and time (year, month, day, weekday(0 for Monday), hour, minute, second, total seconds) rtc.datetime() # check the updated date and time
-
micropython Fading an LED using PWM... with MicroPython!
MENG XI posted a topic in Electronic Gadgets
PWM is one of the crucial peripheral of an MCU, with MicroPython, you can control RTL8722DM to output PWM signal to fade and brighten an LED with ease, here is how, Materials Ameba x 1, LED x 1, Resistor(220ohm) x 1 Steps PWM use pulse width modulation to control output duty cycle and is widely used to control LED brightness and motor. Here we are using an LED to demonstrate how PWM works. Let us connect pin PA_26 to the anode leg of an LED which in series with a current limiting resistor and GND to cathode of the LED as shown below, Then, copy and paste the following code line by line into REPL and hit Enter. If everything is in order, you should be able to see the LED slowly become brighter as you paste another line of code. from machine import Pin, PWM import time p = PWM(pin = "PA_26") # 0 duty cycle thus output 0 p.write(0.0) # 10% duty cycle p.write(0.1) # 50% duty cycle p.write(0.5) # 100% duty cycle p.write(1.0) -
Sometimes we need to exchange some data with another microconrtoller in close range, then we can use I2C for this job, even better, with MicroPython, we can do it in less than 5 lines of code, here is how it's done on ameba RTL8722 from Realtek, Materials Ameba x 1, Arduino UNO x 1 Steps I2C is a very common module on microcontrollers, it only takes 2 wire and able to achieve data rate at up to 3.4Mbps. It works in master-slave model and a master can simultaneously connect to up to 128 slaves, making it a very versatile communication protocol between microcontroller and sensor. Here we are going to use Ameba as an I2C master and Arduino UNO as a slave to achieve I2C send and recv. Before connection, make sure to upload the “Examples -> Wire -> Slave_receiver” example code to Arduino UNO. Connection is shown as follows, here we are using PA_26 as SDA pin and PA_25 as SCL. Note: There is currently 1 set of I2C available to MicroPython user, they are Unit SDA SCL 0 PA_26 PA_25 Then copy and paste the following code line by line into REPL to see their effects. from machine import Pin, I2C i2c = I2C(scl = "PA_25", sda = "PA_26", freq=100000) # configure I2C with pins and freq. of 100KHz i2c.scan() i2c.writeto(8, 123) # send 1 byte to slave with address 8 i2c.readfrom(8, 6) # receive 6 bytes from slave
-
Controlling LED has never been easier with MicroPython on Ameba RTL8722 WiFi and BLE microcontroller, today I am gonna show you just how to blink an LED in 3 different ways! Materials Ameba x 1, LED x 1, Resistor(220ohm) x 1 Steps Blink is one of the best examples to get started with MicroPython. Let us connect pin PB_22 to the anode leg of an LED which in series with a current limiting resistor and GND to cathode of the LED as shown below, Then, copy the following code and press Ctrl + e in REPL to enter the paste mode (for more information about REPL and paste mode, check “Getting started” page). If you are using Tera Term, simply right click on any blank space of the terminal and paste the code into REPL, then press Ctrl + d to execute the code. If everything is order, you should be able to see the LED blink for 3 times in 3 seconds. from machine import Pin a = Pin("PB_22", Pin.OUT) a.value(1) time.sleep_ms(500) a.value(0) time.sleep_ms(500) a.on() time.sleep_ms(500) a.off() time.sleep_ms(500) a.toggle() time.sleep_ms(500) a.toggle()
-
RTL8722 from Realtek supports MicroPython and reading ADC values using Python script in 3 lines, here is an example ADC read potentiometer: • Materials: Ameba x 1, potentiomete x 1 • Steps: Here we connect ameba to a potentiometer to measure its analogue value, the connection is as follows. Copy and paste the following code into REPL. from machine import ADC a = ADC(0) a.read() Values will be obtained and printed on serial terminal once the last line code is executed.
-
This is MicroPython project developed using Ameba RTL8722. MicroPython is offically supported on Ameba RTL8722, so through this demo video, we will see how easy and fast it is to develop a simple server socket on Ameba, which would then control other peripheral to perform other tasks. Here we are using a client socket code running on PC to send a 'Hello, world' string via the WiFi network, Ameba receives it and if it is indeed 'Hello, world' then it will blink the LED. Check out the demo video down below, https://youtu.be/pEMkwvw-r18 Code used: #!/usr/bin/env python3 #PC Client code import socket HOST = '192.168.1.152' # The server's hostname or IP address PORT = 80 # The port used by the server with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) s.send(bytes('Hello, world','utf8')) #Ameba Code from machine import Pin from wireless import WLAN import time import socket wifi = WLAN(WLAN.STA) wifi.connect("MPSSID","upyameba") led = Pin("PB_4",Pin.OUT) s = socket.SOCK() port = 80 data = ' ' s.bind(port) s.listen() conn, addr = s.accept() while True: data = conn.recv(1024) print(data) if data == b'Hello, world': print('turn on LED 1') for i in range(6): led.toggle() time.sleep_ms(200) if not data: break
-
RTL8722 MicroPython SDK alpha release It was annouced that the popular MicroPython project has been ported to ameba RTL8722, with support for peripheral control and Python 3 syntax, you may now control RTL8722 not only with Arduino but also with Python language on the REPL. The SDK is currently in alpha stage and WiFi feature is going to release soon. If you are interested , please find links to the SDK on Github at. https://github.com/ambiot/ambd_micropython?fbclid=IwAR0TxWN06-XT88AfIZbPaBOYJ5fyhh3pMOe-X1RC48Tgari0rXn3nHhEyTo Here are introduction and basic examples to this port, MicroPython Ameba RTL8722 Documentation Realtek's RTL8722 is a ARM Cortex-M33 based, dual-band WiFi and BLE 5.0 capable microntroller that is ideal for many IoT applications. This is a alpha version of the MicroPython port for Ameba RTL8722 platform, details of the platform can be found here https://www.amebaiot.com/en/amebad/ 1. How to build firmware? Currently, this SDK only support building on Cygwin or Linux. Before preceed, please make sure that you have already installed GNU make Open Cygwin terminal/Ubuntu terminal and navigate to "\micropython_amebaD\MicroPython_RTL8722\ports\rtl8722" and then type, $ make 2. How to upload? There are 2 methods to upload Ameba D MicroPython image to your Ameba. 2.1 Release folder In the release folder, there is a Double-Click-Me-to-Upload.cmd file. First we right click on it and select 'Edit', and a notepad will open, now check your Ameba's serial COM port on your PC and update the correct one to the third last line of the file, then save the file and close it. Now press RESET button while holding down UART Download button to enter Download Mode, and you can double click Double-Click-Me-to-Upload.cmd now and the uploading will start shortly. 2.2 port/rtl8722 folder 1st, check your ameba Serial/COM port, make sure Ameba D's port name is correctly updated in the UPLOAD_PATH variable in the Makefile; 2nd, press RESET button while holding down UART Download button to enter Download Mode, Then, type following command, $ make upload 3. How to use MicroPython RTL8722 Port? 3.1 Brief introduction to MicroPython RTL8722 port MicroPython distinguishes itself from other compilation-based platforms (Arduino etc.) with its powerful method of real-time interaction with Microcontroller through a built-in feature -- REPL. REPL stands for Read-Evaluation-Print-Loop, it's an interactive prompt that you can use to access and control your microcontroller. REPL has been equipped with other powerful features like tab completion, line editing, auto-indentation, input history and more. It basically functions like the classic Python IDLE but running on microcontroller. To use REPL, simply open any serial terminal software (most common ones are teraterm, putty etc.) on your PC and connect to your microcontroller's serial port, then set baudrate to 115200 before reset the board, then you will see >>> MicroPython prompt appear on the console. Now you may type in any Python script on REPL as long as it's support by MicroPython and your microcontroller's MicroPython port. 3.2 REPL Hotkeys Ctrl + d : Soft reboot MicroPython will perform software reboot, this is useful when your microcontroller is behaving abnormally. This will also run scripts in 'boot.py' once again. Ctrl + e : Paste mode Paste mode allow you to perform pasting a large trunk of code into REPL at once without executing code line by line. This is useful when you have found a MicroPython library and wish to test it out immediately by copy and paste Ctrl + b : Normal mode This hotkey will set REPL back to normal mode. This is useful if you are stuck in certain mode and can not get out. Ctrl + c : Quick cancel This hotkey help you to cancel any input or interrupt currently running code 4. Peripheral Control -- umachine module MicroPython Ameba D port supports rich peripheral feature through the use of umachine module GPIO To control GPIO, import Pin module through umachine. Here pin PB_18 is used as an example to output logic level 0 and 1 and blink 3 times from umachine import Pin a = Pin("PB_18", Pin.OUT) a.value(1) time.sleep_ms(500) a.value(0) time.sleep_ms(500) a.on() time.sleep_ms(500) a.off() time.sleep_ms(500) a.toggle() time.sleep_ms(500) a.toggle() PWM To use PWM (Pulse Width Modulation), import PWM module through umachine. Here pin PA_26 is used as an example to make an LED to slowly brighten up from umachine import Pin, PWM import time as t p = PWM(pin = "PA_26") for i in range(1000): p.pulsewidth(i) # this and following line will be auto indented on REPL t.sleep_ms(2) Delay and Timing Use the time module import time time.sleep(1) # sleep for 1 second time.sleep_ms(500) # sleep for 500 milliseconds time.sleep_us(10) # sleep for 10 microseconds start = time.ticks_ms() # get millisecond counter Timer Use the Timer module through umachine module There are 4 sets of 32KHz General Timers available to user, Timer 0/1/2/3 from umachine import Timer t = Timer(0) # Use Timer 0/1/2/3 only t.start(2000000, t.PERIODICAL) # Set GTimer fired periodically at duration of 2 seconds, printing text on the terminal RTC Use the RTC (Real Time Clock) module through umachine module from umachine import RTC rtc = RTC() rtc.datetime((2020, 12, 31, 4, 23, 58, 59, 0)) # set a specific date and time (year, month, day, weekday(0 for Monday), hour, minute, second, total seconds) rtc.datetime() # get date and time UART Use the UART module through umachine module from umachine import UART uart = UART(tx="PA_21", rx= "PA_22") uart.write('hello') uart.read(5) # read up to 5 bytes I2C Use the I2C (Inter-Integrated Circuit) module through umachine module Note: I2C only works in master mode. from umachine import Pin, I2C i2c = I2C(scl = "PA_25", sda = "PA_26", freq=100000) # configure I2C with pins and freq. of 100KHz i2c.scan() i2c.writeto(8, 'hello') # send 5 bytes to slave with address 8 i2c.readfrom(8, 6) # receive 5 bytes from slave SPI Use the SPI (Serial Peripheral Interface) module through umachine module, currently only support Master mode, default SPI baud rate is 2MHz. from umachine import SPI spi = SPI(0) # Only support 2 sets of SPI -- 0 and 1 spi # type instance name to check for details of the SPI set spi.write(123) # Write number 123 spi.read()