Introduction: Smart Sanitizer With Magicbit

In this tutorial we will learn about how to make a automatic sanitizer with additional features using Magicbit. We are using magicbit as the development board in this project which is based on ESP32. Therefore any ESP32 development board can be used in this project.


  • Magicbit
  • Ultrasonic Sensor - HC-SR04 (Generic)
  • MG945 metal Servo
  • DFRobot Gravity: Analog Capacitive Soil Moisture Sensor- Corrosion Resistant
  • USB-A to Micro-USB Cable

Step 1: Story

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

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.

Step 2: 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.

Step 3: 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.

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.

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.

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

Step 4: Software Setup

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.

Step 5: Code

#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