Skip to content

Week 8 - Embedded Programming

Group Assignment

  • Compare the performance and development workflows for other architectures

This week we test another D1 mini module which is ESP8266 microcontroller based.

D1 mini pinout

D1mini_pinout

Programming environment setup

Instead of Arduino (C/C++ based), we can use Python to program D1 mini board. And Thonny is a simple develope environment for beginners. You can download Thonny here.

  • .dmg for Mac
  • .exe for Windows

Next we need a driver for connecting D1 mini and Thonny. There’s a CH340C chip on the back side of D1 mini, which is a USB bus conversion chip, it can realize USB to UART interface or USB to printer interface. reference

We downloaded the driver and installed it.

  • CH341SER_MAC.ZIP for Mac
  • CH341SER.EXE for Windows

Lastly connect D1 mini to computer using USB cable, and then open Thonny, click Tools > Options... and change interpreter base on the USB port you are using, also choose MicroPython because this is a simplified Python version that suitable to run in small memory environment.

Thonny_setup

If “MicroPython” shows in Shell, it works. If not, just check your connecting and restart as instructions in red words.

Thonny_work

Project: RFID snesing

The goal is to create a RFID reading device that light up LED when reading card ID and print it out.

Item list: D1 mini (and usb wires), RGB module, RFID modeule (and RFID kits), breadboard, dupont wires

Source of code and library: Flag technology folder

First we need a library of RFID sensing module, and you can find mfrc522.py inside the folder or below. Next open it in Thonny and click Device > Upload current scrip with current nameif seeing message without error and it works.

Thonny_upload

And then run Lab07.py. Wiring shown as below.

wiring

Now you can use RFID card, token or Easycard to test, once success the shell will print out each unique ID.

Note: when trying to test it above my laptop, RFID sensing doesn’t work. I guess maybe the laptop also genrates some signal that disturbs RFID sensing.

Code

from machine import Pin
import mfrc522, time

rfid = mfrc522.MFRC522(0, 2, 4, 5, 14)
led = Pin(15, Pin.OUT)

while True:

    led.value(0)  # turn off LED brfore searching
    stat, tag_type = rfid.request(rfid.REQIDL)  # Searching for RFID

    if stat == rfid.OK:  # find RFID
        stat, raw_uid = rfid.anticoll()  # Read ID
        if stat == rfid.OK:
            led.value(1)  # light up LED after reading ID

            # Transfer ID string from Binary Number to Hexadecimal  
            id = "%02x%02x%02x%02x" % (raw_uid[0], raw_uid[1],
                                       raw_uid[2], raw_uid[3])
            print("Read ID:", id)

            time.sleep(0.5)  # pause LED for a while to see it clear

File

RFID library


Last update: June 22, 2022