Jump to content
Electronics-Lab.com Community

MENG XI

Members
  • Posts

    52
  • Joined

  • Last visited

  • Days Won

    1

MENG XI last won the day on September 23 2021

MENG XI had the most liked content!

1 Follower

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

MENG XI's Achievements

Newbie

Newbie (1/14)

1

Reputation

  1. Machine Learning and TensorFlow Lite for Microcontrollers(TFL4M) While TensorFlow Lite (TFL) for Microcontrollers is designed to run machine learning models on microcontrollers and other devices with only a few kilobytes of memory. The core runtime just fits in 16 KB on an Arm Cortex M3 and can run many basic models. It doesn't require operating system support, nor any standard C or C++ libraries, not even dynamic memory allocation. Ameba and TFL4M Ameba is an easy-to-program hardware platform for developing all kinds of IoT applications. AmebaD is equipped with various peripheral interfaces, including WiFi, GPIO INT, I2C, UART, SPI, PWM, ADC. Through these interfaces, AmebaD can connect with electronic components such as LED, switches, manometer, hygrometer, PM2.5 dust sensors, …etc. What's best? Ameba can be programmed using Arduino IDE, MicroPython and standard C SDK which are all open-source! TFL for Microcontrollers--Hello World Hello World is by far the easiest ML model that can be demo, you only need a LED,or not at all. Here is how we run TFL4M with Hello World, Materials • Ameba D [RTL8722 CSM/DM] x 1 • LED x 1 Example Download the Ameba customized version of TensorFlow Lite for Microcontrollers library at https://github.com/ambiot/ambd_arduino/tree/master/Arduino_zip_libraries. Follow the instructions at https://www.arduino.cc/en/guide/libraries to install it. Ensure that the patch files found at https://github.com/ambiot/ambd_arduino/tree/master/Ameba_misc/ are also installed. Open the example, “Files” -> “Examples” -> “TensorFlowLite_Ameba” -> “hello_world”. Upload the code and press the reset button on Ameba once the upload is finished. Connect the LED to digital pin 10 and ground, ensuring that the polarity is correct. You should see the LED fade in and out rapidly. In the Arduino serial plotter, you can see the output value of the Tensorflow model plotted as a graph, it should resemble a sine wave. Need more resources to get started with Ameba? Check out the 1. Ameba Official website at https://bit.ly/AmebaOfficialWebsite 2. Ameba Forum at https://bit.ly/AmebaForum 3. Ameba Facebook at https://bit.ly/AmebaFB
  2. 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()
  3. 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")
  4. 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
  5. 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().
  6. 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())
  7. 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
  8. 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)
  9. Today is the Chinese Lantern Festival, which is a big festival in Chinese speaking community and also it marks the end of the month-long Chinese New Year celebration, let's see a short clip of Ameba driving e-ink display and playing music https://youtu.be/0uOHViIgqj8
  10. 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
  11. 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()
  12. Introduction Arduino IDE is a convenient tool to write your code and upload to microcontrollers. However, it doesn’t come with debugger function, which means the only way to debug our program is through using debug message through standard output, in this case, Serial Monitor. Debug message is handy and it helps to print out the information the registers holds at a specific time, but once you have done debugging and your program is ready to go, we often have to delete/comment out those debug message code manually, in a big project, this could be problematic as there are just too manny of them. Today, I am going to show you an easy method to turn on / off debug message with only a few lines of code. Method Take a look of the following C code, //#define __DEBUG__ #ifdef __DEBUG__ #define DEBUG(...) printf(__VA_ARGS__) #else #define DEBUG(...) #endif Since all Ameba microcontroller supports printf(), we can re-define printf() to a preprocessor function, in this case, I name it DEBUG(). Whenever you want to print out the debug message, instead of Serial.print() or printf(), you should try using DEBUG() instead, it works just like the previous two, but can be easily enabled or disabled by uncomment //#define __DEBUG__ or just leave the comment syntax there. Let’s look at an example Example code 1 This is a simple code that only print out “Debug msg 1” and “Debug msg 2” alternatively with a 1 second delay in between. Screenshot 2021-01-22 1639441919×1079 173 KB Note that the first line of the code is uncommented, and we are seeing the debug messages actually got printed out using the DEBUG() function. Example code 2 Screenshot 2021-01-22 1641171920×1077 127 KB Note that this is the same code except I keep the first line commented out, and as a result, no debug messages got print out at all. It works! Conclusion All you need to do is to copy the few lines of code from above into the top of your arduino sketch and you may name your custom debug function anything you like and it will work like a charm. Turn on / off is just to keep / remove the // at the first line, very convenient. Hope you like this content, stay healthy and happy coding~
  13. With a BLE5.0 & Dual-band WiFi microcontroller, you will never have to worry about re-compiling the code just to change the WiFi ssid and password. Ameba RTL8722 is a BLE5.0 and dual-band WiFi (2.4G & 5G) Iinternet-of-Things (IoT) developing platform that suit the need for many IoT application scenarios. Taking advantge of its BLE + WiFi combination, we can easily change Ameba's WiFi setting and save us great deal of time. Take a look at the short video below to learn more https://youtu.be/TA4A-VXbImo
  14. 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.
×
  • Create New...