Below is a quick reference guide to Wemos D1 pin mapping for GPIO, I2C and SPI when working from MicroPython.
The visible pin numbers written on the Wemos D1 does not relate to the internal pin numbering.
In MicroPython you need to use the internal pin numbers to create your
Pin objects and interface with the
pins. The following table shows the internal to external pin mapping — together with the default hardware level functions
for the given pin.
|Wemos D1||ESP8266 Pin||Functions|
||GPIO, I2C SCL|
||GPIO, I2C SDA|
||GPIO, SPI SCK (Serial Clock)|
||GPIO, SPI MISO (Master in, Slave out)|
||GPIO, SPI MOSI (Master out, Slave in)|
||GPIO, SPI SS (Slave select)|
||Analog in, via ADC|
I2C positions can be ignored in MicroPython, since the protocol is only available through
a software implementation which works on all GPIO pins.
So, for example if you want to use pin
D6 for output, you could create the interface within MicroPython as follows:
import machine d6 = machine.Pin(12, Pin.OUT)
The Wemos D1 only provides a single analogue input pin, which is accessed by it's ADC (referenced as 0).
import machine adc = machine.ADC(0)
The I2C ports in the table above reflect the pins where hardware I2C is available on ESP8266. However, this is not currently accessible from MicroPython. Instead MicroPython offers a software I2C implementation accomplished by bit-banging, on any combination of GPIO pins. I usually stick them in the same place regardless —
from machine import I2C i2c = I2C(-1, scl=Pin(5), sda=Pin(4))
-1 indicates to MicroPython to use the software I2C implementation.
You can provide any GPIO pins passing in to the
i2c = I2C(-1, scl=Pin(12), sda=Pin(13)) # create I2C using ports D6 and D7 for SCL and SDA respectively
SPI (Serial Peripheral Interface Bus) is a simple protocol, where master and slave device are linked by three
MISO (Master in, Slave out),
MOSI (Master out, Slave in),
SCK (Serial clock, also see as
SS (Slave select) line is used to control communication with multiple
slaves, for more information see this writeup here.
MicroPython provides both software and hardware SPI implementations both via the
To use the hardware implementation of SPI, pass in
1 for the first parameter.
SS) will end up on the pins shown in the
from machine import Pin, SPI spi = SPI(1, baudrate=80000000, polarity=0, phase=0)
The software implementation can be used by passing
-1 as the first parameter, and
pins to use for
from machine import Pin, SPI spi = SPI(-1, baudrate=100000, polarity=1, phase=0, sck=Pin(5), mosi=Pin(4), miso=Pin(0))