Skip to content

Mapping every Digital pin & 9-Bit Binary counter

Lolin1

Summary

This exercise will teach you about every digital pin on the board, as well as pins to avoid when binding them to inputs and output. This will also be a lesson in try/except functions, and notable differences between Python 3.7 and micropython

Requirements

  • 1 x LOLIN D1 MINI PRO board
  • 1 x Micro USB
  • 1-2 x Breadboard
  • 9 x LED of the same color
  • 1 x 1k ohm resistor
  • 10 x cables (minimum)

Building the circuit

Create the following circuit

The numbered pins of the counter are connected in ascending order to each LED, which all share a common rail to ground.

Binary counter

It is important that all of the LEDs are the same colour and a resistor is on the circuit to ground. A common issue is when only one LED is lit due to the difference in colour, which leads to a difference in the lower voltage of the ground rail. This is typically avoided by using LEDs of the same colour as they have the same drain.

Programming the counter

The following program will bind all of the numbered pins in order

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from machine import Pin
import time
pins =[]
for k in range(0,17):    
try:
     if(k == 1 or k == 3 or k == 9 or k ==10):
       print("Cannot bind pin" + str(k)+ "as it is a reserved pin.")
           continue
     else:
           pins.append(Pin(k, Pin.OUT))
           print("Successfully bound pin" + str(k) + ".")
except:
     print("Cannot bind pin" + str(k) + "as it doesn't exist.")

Once that program has run, the following program is used to count correctly:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
i = 0
while True:
       i = i + 1
       j = i
       time.sleep_ms(1000)
       while j >= 0:
         for k in range(9, -1, -1):
           if (j-2**k) >= 0:
             pins[k].on()
             j = j - 2**k
           else:
             pins[k].off()
               if j == 0:   
             j = -1

With this, the LEDs should light up in sequential order as per a binary counter. If not, ensure wiring is correct, and the correct code was used. One common issue is that either time.sleep_ms does not exist, which in that case time.sleep, which sleeps in second intervals, is used instead.

This is the end of Tutorial 7 for LOLIN D1 Mini Pro, you should now know how to bind every numbered pin on the board and how to write scripts to both bind and count the pins.