Smart hand Sanitizer with Magicbit
smart sanitizer
Magicbit Project

Smart Sanitizer with Magicbit

Make a automatic sanitizer with additional features using Magicbit

Components Required

MagicbitUltrasonic Sensor - HC-SR04 (Generic)DFRobot Gravity: Analog Capacitive Soil Moisture Sensor- Corrosion ResistantUSB-A to Micro-USB Cable


Hi guys, today we will learn about how to make a smart hand sanitizer using Magicbit with Arduino IDE.


smart sanitizer pic 1


In these days all you know about there is a global issue which is corona. So in this situation hygiene is one of most important thing. Therefore we used sanitizers to clean our hands. But, to put out sanitizing liquid we have to push the sanitizer bottle’s head. When everyone tries to push that head it can be cause to spread germs. To solve that problem we introduced very simple solution with magicbit. That is this smart sanitizer.

Lets look at how we made this.

Theory and Methodology

The theory is simple. When you reached to the sanitizer bottle it will detect you by using ultrasonic sensor. When you close it to than some certain distance, the Magicbit gives the signal to servo motor to rotate. So when servo motor is rotates the bottle’s head pushed and sanitizing liquid put out from bottle. When the liquid of the bottle is reduced than some certain level, it detect by the Magicbit by using soil moisture sensor. This sensor is capacitive. Therefore we can measure the liquid level by touching bottle wall rather than putting sensor to liquid. This is an additional feature.

Using following link you can learn more about sonar, servo, soil moisture and Magicbit dev. board.


Hardware Setup

This have two sections. First one is build the circuit and second one is build the mechanism. We connect two sensors and servo motor to three expansion ports of the Magicbit. The complete circuit diagram is shown below.


smart sanitizer pic 2


When you build the complete circuit you have go to the second section. To push bottle’s head we used servo motor with it’s arm clips. Those clip rotates and it hits with head of the bottle. So head pushes down. In this mechanism we converts servo’s rotating motion to bottle head’s linear motion. You can use any kind of mechanism using servo to fulfill this need. Below pictures show our mechanism. you can build that your own.


smart sanitizer pic 3


Note: If you used small mg90 servo’s, it may don’t have enough torque (power to push the bottle’s head to downside. So when you choosing the servo motor make sure your have have enough torque).

To measure the moisture, we used capacitive moisture sensor which includes in Magicbit dev. kit. But you can get that from externally. When you connect it into the bottles surface make sure it will touch the bottle’s wall surface hardly. Otherwise it don’t gave high deviation when liquid level is going down.


smart sanitizer pic 4


To detects the hands we set the ultrasonic sensor near to the bottle as facing to upper side with small angle.

Software Setup


smart sanitizer pic 5


To program the Magicbit we used Arduino IDE. The algorithm is simple. When we powered on the Magicbit it will get the distance to nearest object from sonar. Then it checks that nearest object is near than certain level distance. If so it will check the bottle is opened or closed. if it opened then don’t do anything. else open the bottle. We used some delay to cancel the noises and improve the accuracy of the readings.

When using soil moisture sensor make sure it is calibrated. To do that first we expose the sensor to air. At that time we mark the analog read which receives from Magicbit. Then we get another reading when the sensor is touching the bottles surface. In that case make sure the bottle have completely fully from liquid. Get the middle of those two numbers as threshold. When the reading is higher than that value it implies the bottle is over by generating sound from buzzer.

To upload the code connect Magicbit to the computer using data cable. Select the correct COM port and board type and upload the code. enjoy yourself.


#include <NewPing.h>
#include <ESP32Servo.h>
#define TRIGGER_PIN  21
#define ECHO_PIN     22
#define MAX_DISTANCE 200
#define SENSOR 32;

int output_value ;
int distance;
int count=0;
bool Open=false;
Servo Servo; 
void setup() {
  Servo.attach(26); // Defines on which pin is the servo motor attached
   pinMode(32,INPUT);//moisture sensor attached pin
   pinMode(25,OUTPUT);// buzzer attached pin 
void loop() {
    output_value= analogRead(SENSOR);
    if(output_value<2530){//change this value after calibration
 distance = sonar.ping_cm();
  if( distance>0 && distance<=30  && Open==false){

  for(int i=130;i>=90;i--){  //push head

  else if(  (distance>60 || distance==0) && Open==true){
   for(int i=90;i<=130;i++){  //release head
  tone( 25 ,4186,500); //C Note



YouTube video by clicking here:
YouTube Video

Related Posts
Leave a Reply