Skip to content
International Shipping
Wish Lists Cart
0 items

#19 Hungry Piggy Bank Project With PicoBricks

08 Nov 2023
#19 Hungry Piggy Bank Project With PicoBricks

Ultrasonic sensors send out sound waves at a frequency that our ears cannot detect and produce distance information by calculating the return time of the reflected sound waves. We, the programmers, develop projects by making sense of the measured distance and the changes in distance. Parking sensors in the front and back of the cars are the places where ultrasonic sensors are most common in daily life. Do you know the creature that finds its way in nature with this method? Because bats are blind, they find their way through the reflections of the sounds they make.

Many of us like to save money. It is a very nice feeling that the money we save little by little is useful when needed. In this project, you will make yourself a very enjoyable and cute piggy bank. You will use the servo motor and ultrasonic distance sensor while making the piggy bank.

Details and Algorithm

HC-SR04 ultrasonic distance sensor and SG90 servo motor will be used in this project. As the user places money in the hopper of the piggy bank, the distance sensor will detect the proximity and signal PicoBricks. PicoBricks will operate the servo motor and raise the arm, drop the money into the piggy bank, and then the arm will go down again.


1X PicoBricks
1X HC-SR04 Ultrasonic Sensor
1X Servo Motor
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

You can access the original files and construction stages of the project by clicking here. Unlike the project in this link, we will use the HC-SR04 ultrasonic distance sensor. You can download the updated 3D drawing files according to the HC-SR04 ultrasonic distance sensor from this link and get 3D printing.

1: Fix the plastic apparatus of the servo motor to the piggy bank arm with 2 screws.

2: Fix the second part of the piggy bank arm with the M3 screw and nut to the first part where the hopper is.

3: Pass the servo motor cable and place it in its slot.

4: Place the servo motor and its housing on the body of the piggy bank. You can use hot glue here.

5: Place the ultrasonic distance sensor in the piggy bank body and fix it with hot glue.

6: Attach the piggy bank arm to the servo motor and fix it to the top cover with M3 screws.

7: Fix the piggy bank arm to the body with M2 screw.

8: Plug the cables of the servo motor and ultrasonic distance sensor and connect the power cables.

9: According to the circuit diagram, connect the cables of the servo motor and ultrasonic distance sensor to the pico.

10: Plug Pico’s USB cable and reassemble the cables and attach the bottom cover. That is all.

MicroBlocks Codes of the PicoBricks

You can access the Microblocks codes of the project by dragging the image to the Microblocks Run tab or click the button.



Microblocks Run Tab

MicroPython Codes of the PicoBricks


from machine import Pin, PWM
import utime
#define the libraries
servo = PWM(Pin(21, Pin.OUT))
trigger = Pin(15, Pin.OUT)
echo = Pin(14, Pin.IN)
#define the input and output pins

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
    print("The distance from object is ", distance, "cm")
    return distance
#calculate distance

while True:
    if int(getDistance()) <= 5:  # if the distance variable is less than 5
        utime.sleep(0.3)  # wait

Arduino C Codes of the PicoBricks


#include <Servo.h>
#define trigPin 15
#define echoPin 14
//define the libraries
Servo servo;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //define the input and output pins
  servo.attach(21); //define the servo pin

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  //calculate distance

  if (distance < 5) {    //if the distance variable is less than 5
    Serial.println(" cm");
  else if (distance > 5) {   // if the distance variable is greater than 5
    Serial.println(" cm");
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