Virtual Raspberry Pi Pico simulator - 7 segment display and a toggle switch simulation along with the Raspberry Pi Pico.
2021-05-18_23h44_29.png

Connection diagram of Raspberry Pi Pico and 7 segment display with a switch on Raspberry Pi Pico simulator

Let us see how to connect a 7 segment display to a Raspberry Pi Pico. We will also connect a slide switch to the raspberry Pi Pico emulator so that we can toggle between incrementing and decrementing the number on the 7 segment display.

Project simulation (How it will look at the end)

The raspberry Pi Pico simulation with the Raspberry Pi Pico and the 7 segment display. 

wokwi Raspberry Pi Pico simulator 1.gif

7 segment display interface on Wokwi Raspberry Pi Pico Simulator

Connection diagram / wiring diagram

2021-05-18_23h44_29.png

The link to the project is here: https://wokwi.com/arduino/projects/298014884249993738

You can play around with the Raspberry Pi Pico simulator using th link above. No login or installation needed!

Code for the project

Below is the code for the project. Please note that, you can always use the project link above. This is one of the cool thing. You can find the code, connection diagram and also play with the simulations on the raspberry Pi Pico simulator :) 

        /**
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"

/*
  Our 7 Segment display has pins as follows:
  --A--
  F   B
  --G--
  E   C
  --D--
  By default we are allocating GPIO 2 to A, 3 to B etc.
  So, connect GOIP 2 to pin A on the 7 segment LED display etc. Don't forget
  the appropriate resistors, best to use one for each segment!
  Connect button so that pressing the switch connects the GPIO 9 (default) to
  ground (pull down)
*/

#define FIRST_GPIO 2
#define BUTTON_GPIO (FIRST_GPIO+7)

// This array converts a number 0-9 to a bit pattern to send to the GPIO's
int bits[10] = {
        0x3f,  // 0
        0x06,  // 1
        0x5b,  // 2
        0x4f,  // 3
        0x66,  // 4
        0x6d,  // 5
        0x7d,  // 6
        0x07,  // 7
        0x7f,  // 8
        0x67   // 9
};

/// \tag::hello_gpio[]
void setup() {
    Serial1.begin(115200);
    Serial1.println("Hello, 7segment - press button to count down!");

    // We could use gpio_set_dir_out_masked() here
    for (int gpio = FIRST_GPIO; gpio < FIRST_GPIO + 7; gpio++) {
        gpio_init(gpio);
        gpio_set_dir(gpio, GPIO_OUT);
        // Our bitmap above has a bit set where we need an LED on, BUT, we are pulling low to light
        // so invert our output
        gpio_set_outover(gpio, GPIO_OVERRIDE_INVERT);
    }

    gpio_init(BUTTON_GPIO);
    gpio_set_dir(BUTTON_GPIO, GPIO_IN);
    // We are using the button to pull down to 0v when pressed, so ensure that when
    // unpressed, it uses internal pull ups. Otherwise when unpressed, the input will
    // be floating.
    gpio_pull_up(BUTTON_GPIO);

    int val = 0;
    while (true) {
        // Count upwards or downwards depending on button input
        // We are pulling down on switch active, so invert the get to make
        // a press count downwards
        if (!gpio_get(BUTTON_GPIO)) {
            if (val == 9) {
                val = 0;
            } else {
                val++;
            }
        } else if (val == 0) {
            val = 9;
        } else {
            val--;
        }

        // We are starting with GPIO 2, our bitmap starts at bit 0 so shift to start at 2.
        int32_t mask = bits[val] << FIRST_GPIO;

        // Set all our GPIO's in one go!
        // If something else is using GPIO, we might want to use gpio_put_masked()
        gpio_set_mask(mask);
        sleep_ms(500);
        gpio_clr_mask(mask);
    }
}
/// \end::hello_gpio[]

void loop() {}
    

Ideas for you to create related projects

  • Display hexadecimals as well - A, B, C, D, E and F
  • Can you add one more 7 segment element and make the project count from 00 to 99?

Support on Raspberry Pi Pico simulator topics

Hop on to Discord server! you can also leave a comment here too. Please visit Electrotantra YouTube channel for more videos on the Arduino and Raspberry Pi Pico simulators.

share Open-Tech
Hardware enthusiast with ample interest in Arduino projects

Categories