Skip to content
International Shipping
Wish Lists Cart
0 items

#22 Digital Ruler Project With PicoBricks

03 Nov 2023
#22 Digital Ruler Project With PicoBricks

Many tools are used to measure length. One of the first that comes to mind is rulers.Our measuring instrument differs according to the place and size to measure. Tape measures are used in construction and architecture, and calipers are used for small objects that require millimeter precision. In addition, if it is desired to measure an area that needs both large and precise measurement, distance meters working with laser and infrared systems are used. Ultrasonography devices used in the health sector also work with the same logic, but convert their measurements into visuals.

In our project, we will use PicoBricks and an ultrasonic sensor to prepare a digital ruler that will display the distance value on the OLED screen when the button is pressed. Ultrasonic sensors detect distance according to the return times of the sound waves they emit.

Details and Algorithm

When the program starts, instructions are displayed on the OLED screen. After the user presses the button, a measurement is taken. The red LED stays on during the measurement, and then turns off. The measured value is added to the distance from the tip of the sensor to the back of the box. The final calculated value is displayed on the OLED display.


1X PicoBricks
1X HC-SR04 Ultrasonic Sensor
Jumper Cables
Easy Connection Cables

Wiring Diagram

You can code and run Picobricks’ modules without wiring. If you are going to use the modules by separating them from the board, you should make the module connections with grove cables.

Construction Stages of the Project

To prepare the project, you need double-sided foam tape, a utility knife, a waste cardboard box of approximately 15x10x5 cm.

1) Cut the holes for the ultrasonic sensor, OLED screen, button LED module, buzzer, battery box to pass the cables with a utility knife.

2) Hang all the cables inside the box and attach the back of the modules to the box with double-sided foam tape. Connect the trig pin of the ultrasonic sensor to the GPIO14 pin and the echo pin to the GPIO15 pin. You should connect the VCC pin to the VBUS pin on the Picoboard.


3) After completing the cable connections of all modules, you can insert the 2-battery box into the power jack of the Picoboard and turn on the switch. That’s it for the digital ruler project!

Microblocks Run Tab

MicroPython Codes of the PicoBricks


from machine import Pin, PWM, I2C
from utime import sleep
from picobricks import SSD1306_I2C
import utime

# Define the libraries
redLed = Pin(7, Pin.OUT)
button = Pin(10, Pin.IN, Pin.PULL_DOWN)
buzzer = PWM(Pin(20, Pin.OUT))
trigger = Pin(15, Pin.OUT)
echo = Pin(14, Pin.IN)

# Define input and output pins
WIDTH = 128

# OLED screen settings
sda = machine.Pin(4)
scl = machine.Pin(5)
i2c = machine.I2C(0, sda=sda, scl=scl, freq=1000000)

# Initialize digital pin 4 and 5 as an OUTPUT for OLED communication
oled = SSD1306_I2C(128, 64, i2c)

measure = 0
finalDistance = 0

def getDistance():
    while echo.value() == 0:
        signaloff = utime.ticks_us()
    while echo.value() == 1:
        signalon = utime.ticks_us()
    timepassed = signalon - signaloff
    distance = (timepassed * 0.0343) / 2
    return distance

# Calculate the distance
def getMeasure(pin):
    global measure
    global finalDistance
    for i in range(20):
        measure += getDistance()
    finalDistance = (measure/20) + 1
    oled.text(">Digital Ruller<", 2, 5)
    oled.text("Distance " + str(round(finalDistance)) + " cm", 0, 32)

    # Print the specified distance to the specified x and y coordinates on the OLED screen
    measure = 0
    finalDistance = 0

# Sound the buzzer
button.irq(trigger=machine.Pin.IRQ_RISING, handler=getMeasure)

Arduino C Codes of the PicoBricks


#include <Wire.h>
#include "ACROBOTIC_SSD1306.h"
#include <NewPing.h>
// Define the libraries
#define TRIGGER_PIN  15
#define ECHO_PIN     14
#define MAX_DISTANCE 400
#define T_B 493
int distance = 0;
int total = 0;

void setup() {
  pinMode(7, OUTPUT);
  pinMode(20, OUTPUT);
  pinMode(10, INPUT);
  // Define input and output pins

void loop() {
  if(digitalRead(10) == 1) {
    int measure = 0;
    digitalWrite(7, HIGH);
    tone(20, T_B);
    for (int i = 0; i < 20; i++) {
      measure = sonar.ping_cm();
      total = total + measure;
    distance = total / 20 + 6; // Calculate the distance
    digitalWrite(7, LOW);
    oled.setTextXY(2, 1);
    oled.putString(">Digital Ruler<");
    oled.setTextXY(5, 1);
    oled.putString("Distance: ");
    oled.setTextXY(5, 10);
    String string_distance = String(distance);
    oled.setTextXY(5, 12);
    oled.putString("cm"); // Print the calculated distance on the OLED screen
    measure = 0;
    distance = 0;
    total = 0;

GitHub Project Page
Prev Post
Next Post

Thanks for subscribing!

This email has been registered!

Shop the look
Choose Options

Edit Option

Have Questions?

Back In Stock Notification


Product SKUDescription Collection Availability Product Type Other Details

Terms & Conditions

What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
this is just a warning
Shopping Cart
0 items