Binary to Hexadecimal and Denary Converter on Raspberry Pi Pico

by Guitarman9119 in Circuits > Microcontrollers

2667 Views, 21 Favorites, 0 Comments

Binary to Hexadecimal and Denary Converter on Raspberry Pi Pico

Binary to Hexadecimal and Denary converter on Raspberry Pi Pico

The Raspberry Pi Pico is an awesome microcontroller, so much that I started to use it in my computer / engineering classes I teach.

This is project is a Binary to Hexadecimal / denary converter learning board based on the Raspberry Pi Pico. The goal of this board is to help you while learning the fundamentals of number systems used in computers and serve as a great soldering practice board.

In terms of the Raspberry Pi Pico - You will learn how to connect the 16X2 Character LCD, setting up pins as inputs for the buttons and output for the LEDs, and learning MicroPython, related to the Pico.

The learning board will convert an 8-bit binary number to Decimal or Hexadecimal.

The board has 11 inputs, which include 8 buttons to select from bit 0 - 7, an add, subtract and reset button. This will allow you set the bit high or low in an 8 bit number which will indicate through the LED - by turning LED on for 1 / High and off for a 0 / Low.

The Pico will take the 8 bit number and convert it into decimal and hexadecimal.

The 16x2 character LCD display will be used as output to indicate the results. The LED will display the decimal, hexadecimal and binary number.

Supplies

Sequence 01.00_00_01_08.Still001.png

To make this instructable as detailed as possible, I will include links from AliExpress; these are not affiliated links but will guide you on where to start. You might already have some of the components.

Many of these parts can be changed as needed.

Breadboard:

- Raspberry Pi Pico + Header Pins + Cable - AliExpress

- 16 x 2 Character LCD display - AliExpress

- 11 Tactile push buttons 12x12 mm - AliExpress and caps - AliExpress

- 8 LED's - AliExpress

- Breadboard - AliExpress

- Breadboard Wires - AliExpress

- 330 Ohm Resistors - AliExpress

- Male-Female , Male-Male jumpers - AliExpress

PCB:

- Raspberry Pi Pico + Header Pins + Cable - AliExpress

- 16 x 2 Character LCD display - AliExpress

- 11 Tactile push buttons 6x1 mm - AliExpress

- 8 LED's - AliExpress

- 330 Ohm resistors - AliExpress

- Male-Female , Male-Male jumpers - AliExpress

Tools:

Soldering Iron, Wire stripper/cutter

Optional:

3D printer, Laser cutter to make an enclosure

Circuit Diagram

ezgif.com-gif-maker.gif
schematic.png
BREAD_BOARD.JPG
Screenshot 2022-06-05 111219.png

To following along with this project on the breadboard I have included the schematic diagram. One side of the buttons will connect to the 3.3V rail, and we will use an internal Pull-down resistors on the pins of the Pico. We will connect eight LEDs to the Pico setting the pins to output and using current limiting resistors.

The 16x2 I2C LCD will be connected to GP0 and 1 using the I2C0 communication protocol.

PCB Design

ezgif.com-gif-maker (1).gif
LEARNING_BOARD.png
PCB_I2C_through_hole_LEDs.png
PCB_no_i2c.png
PCB_WS2812b.png

I decided to make 3 separate PCB files. Which is available in the following GitHub repository.

  • PCB 1 - with I2C LCD support, 8 Through hole LEDs
  • PCB 2 - without I2C using 4 data pins, 8 Through hole LEDs
  • PCB 3 - with I2C LCD support and 4 data pins, WS2812b LEDs

If you want to order great Affordable PCB Prototype Service you can look at PCBWay

Installing MicroPython

ezgif.com-gif-maker (2).gif

Start with your Pico unplugged from USB. Hold down the BOOTSEL button, and while continuing to hold it (don't let go!), plug the Pico into USB. A short GIF above illustrates this step. Continue to hold the BOOTSEL button until the RPI-RP2 drive appears.

Drag the rp2-pico-20220117-v1.18.uf2 file to RPI-RP2.

Alternative method will be using Thonny to install MicroPython. If you are completely new to the Raspberry Pi Pico do not fear there are many videos and tutorials on getting started and I have included a tutorial here which you can find on my YouTube channel (Link).

Code Explanation

To code this project is straight forward. We will setup the pins to have 8 outputs for the LEDs and 11 inputs for the buttons.

Libraries

Libraries needed: https://github.com/T-622/RPI-PICO-I2C-LCD

We start of by importing all the necessary libraries.

from machine import I2C, Pin
import utime
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd

To setup the I2C we need to know the I2C address. To get the address of your I2C LCD run the following script with LCD connected.

import machine
sda = machine.Pin(0)
scl = machine.Pin(1)
i2c = machine.I2C(0,sda=sda,scl=scl, freq=400000)
print(i2c.scan())

This will give your I2C Address Now we can setup the I2C 16x2 character display:

I2C_ADDR     = 63
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)

The next step we will create a list of GP pins our LEDs will be connected to.

LED_pins = [9,8,7,6,5,4,3,2]

Create an empty lists to set up and initialize pins as output by looping through the LED_pins list.

LED = []
for x in range(0,8):
    LED.append(Pin(LED_pins[x], Pin.OUT))
    LED[x].value(0)

We will repeat the same process for the buttons

Button_pins = [17,16,15,14,13,12,11,10,18,19,20]
Button = []
# Loop to assign GPIO pins and setup input and outputs
for x in range(0,11):
    Button.append(Pin(Button_pins[x], Pin.IN, Pin.PULL_DOWN))
    Button[x].value(0)

We will then create a counter that will allow us to store the denary value of the active bits that is High using the Decimal to Binary conversion table.

counter = 0
#Decimal to Binary conversion table 
table = [1,2,4,8,16,32,64,128]

Before looking at the function written to control LED's and display the converter values, we first will look at the main loop.

We will first loop through the 8 buttons to see if any button is pushed. The buttons is connected to the 3.3V output of the Pico and using the internal pull-down resistors of the Pico to keep the pin low. When a button is pressed the value will go to 1.

We will first check if the LED value is 1 and button 1, which means the bit was already set, which we will then set the button value to zero, remove that bit decimal value from the table to counter. We print out the counter, and after a short delay we call the function display(), which we pass the value of counter.

If the LED was zero along with the button we will add the bit decimal value from the table to counter, print the counter value, followed by a short delay and calling the display() function passing it the value of counter.

while True: 
    for x in range(0,8):
        if Button[x].value() == 1:
            utime.sleep(0.1)
            if LED[x].value() == 1 and Button[x].value() == 1:
                Button[x].value(0)
                counter = counter - table[x]
                print(counter)
                utime.sleep(0.3)
                display(counter)
            
            elif LED[x].value() == 0 and Button[x].value() == 0:
                counter = counter + table[x]
                print(counter)
                utime.sleep(0.3)
                display(counter)

We then check the other 3 inputs which will be our Reset button to set our counter to zero and call the display() function, the other two buttons will add 1 to the counter or substract 1 from the counter. This will allow you to add one to counter between the value of 0 to 255.

#Reset Button
        if Button[8].value() == 1:
            utime.sleep(0.1)
            counter = 0
            print(counter)
            utime.sleep(0.3)
            display(counter)
        
#Add
        if Button[9].value() == 1:
            utime.sleep(0.1)
            counter += 1
            utime.sleep(0.3)
            display(counter)

#Subtract
        if Button[10].value() == 1 and counter > -1:
            utime.sleep(0.1)
            counter -= 1
            utime.sleep(0.3)
            display(counter)

The main function display, will control the LCD and what we display to the LCD. We create an 8 bit binary digit from the counter and store it in a list LED_ON_OFF. We map this list to a string to be used to display to LCD. We then convert the counter to a hexadecimal value and then we can loop through the LED_ON_OFF list to set the LEDs High or Low depending on the counter value.

Then using the LCD library methods we write all the information to the LCD screen.

def display(counter):

    #create a binary value from the counter value passed to check which LED should be turned on or off
    LED_ON_OFF = [1 if counter & (1 << (7-n)) else 0 for n in range(8)]

    #map the list together to a string to display
    binary_display = ''.join(map(str,LED_ON_OFF))

    #reverse the order of the LED list
    LED_ON_OFF.reverse()

    #Loop through values in LED_ON_OFF list to set LEDs High or Low
    for i in range(8):
        LED[i].value(int(LED_ON_OFF[i])) 
 
    #string formatting
    #converter the counter to hexadecimal
    hexa = hex(counter).replace("0x", "")
    #hexa = hexa[2:]
    while len(hexa) < 2:
        hexa = '0' + hexa
    hexa = '0x' + hexa


    decimal = str(counter)
    while len(decimal) < 3:
        decimal = '0' + decimal

    lcd.move_to(0,0)
    lcd.putstr("Dec:")
    lcd.putstr(decimal)

    lcd.move_to(8,0)
    lcd.putstr("Hex:")
    lcd.putstr(str(hexa).upper())

    lcd.move_to(0,1)
    lcd.putstr("Bin:")
    lcd.putstr(binary_display)
    utime.sleep(0.1)

The code is straightforward when using the few Python tricks

Future Improvement

IMG_4205.JPG

There are always ways to improve a project. To add to this project, I want to add an enclosure to which the board can connect, including a power bank to allow you to use the project without the need for a USB plugged in.

If you would like to collaborate, please reach out. I hope you found this instructable useful.