Wemos D1 pin numbers & functions | Pin mapping and I2C/SPI from MicroPython

Below is a quick reference guide to Wemos D1 pin mapping for GPIO, I2C and SPI when working from MicroPython.

Pin mapping

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
D0 16 GPIO
D5 14 GPIO, SPI SCK (Serial Clock)
D6 12 GPIO, SPI MISO (Master in, Slave out)
D7 13 GPIO, SPI MOSI (Master out, Slave in)
D8 15 GPIO, SPI SS (Slave select)
A0 A0 Analog in, via ADC
RX 3 Receive
TX 1 Transmit

The hardware 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))

The -1 indicates to MicroPython to use the software I2C implementation.

You can provide any GPIO pins passing in to the scl and sda parameters.

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 data lines MISO (Master in, Slave out), MOSI (Master out, Slave in), SCK (Serial clock, also see as M-CLK)

The 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 machine.SPI class.

To use the hardware implementation of SPI, pass in 1 for the first parameter. Pins M-CLK, MISO, MOSI (and SS) will end up on the pins shown in the table (D5, D6, D7 and D8 respectively).

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 sck, mosi and miso.

from machine import Pin, SPI
spi = SPI(-1, baudrate=100000, polarity=1, phase=0, sck=Pin(5), mosi=Pin(4), miso=Pin(0))

Continue reading

Lucky Cat Spinning-arm Display

This build started as something simple: a lucky cat which would turn on and off automatically in response to some event. Since lucky cats are associated with good fortune the idea was to make one do this every time I got paid. This was working pretty well but unfortunately, after ... More


comments powered by Disqus