At some point of time we built together with my daughter a simple mechanical elevator and then I had an idea – why not to make it motorized and have just couple of buttons and have a bit of experience with the H bridges and motors.

So what I did initially – I went shopping. Original lego of course is super expensive and I opted out for the cheap copies from aliexpress:



XL motor was the latest addition, since it appeared my cabin was too heavy and M motor was not enough to lift it.
Then, I ordered L9110S DC Motor Drive Module from amazon, somehow my researches narrowed onto that module and I made the dumb mistake I keep doing again and again – NEVER buy crappy pathetic ICs. I don’t understand why I am as IC design engineer keep buying crappy chips.
Honestly, currently I don’t even remember what was wrong with that chip – just vague memories how any time I was putting the breadboard together I constantly was having issues with everything and I think I got like two modules killed for who know which reason. Oh and the output current was not enough to handle the XL motor.
After quite a long time and struggle I decided to do the right way and to make a proper PCB. But before that some photos of the fun lego building process







A couple of notes from my lego building experience:
- Plastic long pin (axle) is quite soft and not suitable for putting momentum enough to move my cabin. I replaced it by threaded metallic pin I’ve got from the home depot store.
- Making 3-4 gears system around all 4 corners was a mistake, the leveling of the cabin is a PITA and prone to failures. I should have looked how 3d printers are made or CNC machines, if I needed to start such project again I would use their approach (Put a vertical threaded pin and connect the motor to the nut hooked to the cabin).
- Don’t count to use the same voltage source for the motor and the control. With the XL motor the back surge/spike was that large that it killed microcontroller and the voltage regulator a couple of times OR it was just resetting my uc and I could not understand what’s happening.
For the motor driver I took TI DRV8837 and for the uC I picked PIC16F18046 of Microchip simply because it was available on digikey for the okaish price, plus it has peripheral pin select module (PPS) thingie which allows you to configure almost any of pins for any of peripheral function. I was just curious I guess.
And for the voltage regulator I picked something more modern and with small quiescent current – MIC5504.
Both voltage regulator and the microcontroller went to my weird IC behavior database, more details are available at the link.
The schematic of the control board is below:
Two driver ics since I used two motors – one for the cabin lift and one for the door opening. Also I made it possible to connect the higher voltage supply as a source for the voltage regulator, but in the end this feature was really bad with the XL motor – you want to completely separate this super noisy motor voltage domain from any other domains.
The pcb layout:

I’ve added plenty of vias and tried to make vdd separations to be really good and for TI chips I followed suggestions of their datasheets.

That how this PCB looks embedded in the lego environment:

And some of photos of more complete builds:







From the code perspective it rather simple – we have to move cabin up and down (PWM), open and shut the door (PWM). Do some fancy lighting and I used tactical switches as sensor of the cabin position.
/* * File: main.c * Author: sarge * diymicro.org * Created on April 7, 2024, 7:50 PM */ #include <stdio.h> #include <stdlib.h> #define _XTAL_FREQ 1000000 //The speed of your internal(or)external oscillator // PIC16F18046 Configuration Bit Settings // 'C' source line config statements // CONFIG1 #pragma config FEXTOSC = OFF // External Oscillator Selection bits (Oscillator not enabled) #pragma config RSTOSC = HFINTOSC_1MHz// Reset Oscillator Selection bits (HFINTOSC (1MHz)) #pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2) #pragma config VDDAR = LO // VDD Range Analog Calibration Selection bit (Internal analog systems are calibrated for operation between VDD = 1.8 - 3.6V) // CONFIG2 #pragma config MCLRE = EXTMCLR // Master Clear Enable bit (If LVP = 0, MCLR pin is MCLR; If LVP = 1, RA3 pin function is MCLR) #pragma config PWRTS = PWRT_OFF // Power-up Timer Selection bits (PWRT is disabled) #pragma config WDTE = OFF // WDT Operating Mode bits (WDT disabled; SEN is ignored) #pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out reset disabled) #pragma config DACAUTOEN = OFF // DAC Buffer Automatic Range Select Enable bit (DAC Buffer reference range is determined by the REFRNG bit) #pragma config BORV = HI // Brown-out Reset Voltage Selection bit (Brown-out Reset Voltage (VBOR) set to 1.9V) #pragma config ZCD = OFF // ZCD Disable bit (ZCD module is disabled; ZCD can be enabled by setting the ZCDSEN bit of ZCDCON) #pragma config PPS1WAY = OFF // PPSLOCKED One-Way Set Enable bit (The PPSLOCKED bit can be set and cleared as needed (unlocking sequence is required)) #pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset) // CONFIG3 // CONFIG4 #pragma config BBSIZE = BB512 // Boot Block Size Selection bits (512 words boot block size) #pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled) #pragma config SAFEN = OFF // Storage Area Flash (SAF) Enable bit (SAF disabled) #pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block is NOT write protected) #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block is NOT write protected) #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register is NOT write protected) #pragma config WRTD = OFF // Data EEPROM Write-Protection bit (Data EEPROM is NOT write-protected) #pragma config WRTSAF = OFF // Storage Area Flash (SAF) Write Protection bit (SAF is NOT write protected) #pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR. MCLRE Configuration bit is ignored) // CONFIG5 #pragma config CP = OFF // Program Flash Memory Code Protection bit (Program Flash Memory code protection is disabled) #pragma config CPD = OFF // Data EEPROM Code Protection bit (EEPROM code protection is disabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include <xc.h> volatile __bit door_button = 0; volatile __bit door_sensor = 0; volatile __bit bottom_sensor = 0; volatile __bit top_sensor = 0; volatile __bit nothing_happening = 0; volatile __bit cabin_position = 0; //0 the elevator is at the bottom, 1 - at the top volatile __bit door_position = 0; //0 the door is closed, 1 - the door is opened unsigned char door_status = 0; void all_motors_off(void); void led_off(void); void led_door_opened(void); void led_cabin_moving(void); void door_open(void); void door_close_slow(void); void door_close_fast(void); void lift_up(void); void lift_dn(void); void door_check(void); void cabin_check(void); void delay_1s(void); /* * */ int main() { //disable all the analog functions (dont need them) //IMPORTANT: do not use A2 pin, for some reason it resets whole chip no matter what ANSELA = 0; ANSELB = 0; ANSELC = 0; nothing_happening = 1; //initially nothing happening //-------------buttons definitions ---------------- //buttons/sensors set we have WPUA5 = 1; //enable pull-up for ra5 (dont have it on the pcb) TRISA5 = 1; //this one doesnt work well in interrupt -> top sensor TRISC0 = 1; //that will be our button TRISC1 = 1; //door sensor TRISC2 = 1; //bottom sensor //IOCAN2 = 1; //looking for negative edge at RA2 IOCCN0 = 1; //looking for negative edge at RC0 //IOCCN1 = 1; //looking for negative edge at RC1 //IOCCN2 = 1; //looking for negative edge at RC2 IOCCF = 0; //-------------end of buttons definitions --------- //-------------LED outputs ------------------------ TRISB5 = 0; //LED1 RB5 TRISB6 = 0; //LED2 RB6 LATB5 = 1; LATB6 = 1; led_off(); //-------------end of LED outputs ----------------- //-------------PWM motor outputs ------------------ //RC3 - main motor up, for that mode we dont need any PWM //RC6 - main motor down -> no need here, rather we will slow down rc3 //RC7 - door motor open //RB7 - door motor close //-------------End of PWM motor outputs ----------- all_motors_off(); //by default everything is disabled //led_cabin_moving(); GIE = 1; PEIE = 1; IOCIE = 1; //SLEEP(); //go to sleep imediately, awaiting the button press //cabin_check(); //door_open(); //lift_dn(); //TRISC3 = 0; //PORTCbits.RC3 = 1; while (1) { //checking if the door button being pressed if (door_button) { __delay_ms(200); __delay_ms(200); __delay_ms(200); __delay_ms(200); if (!PORTCbits.RC0) { cabin_check(); if (!cabin_position) //cabin at the bottom { //now check that door is closed if (door_position) //if it is opened we need to close it first { door_close_slow(); __delay_ms(200); __delay_ms(200); __delay_ms(200); door_position = 0; all_motors_off(); }//if door is opened led_cabin_moving(); lift_up(); do { //empty loop } while (PORTAbits.RA5); //until cabin won't reach the top //cabin_position = 1; //cabin at the top led_door_opened(); //put the door out to hold a cabin door_open(); __delay_ms(200); __delay_ms(200); if (!PORTCbits.RC1) all_motors_off(); else { __delay_ms(200); all_motors_off(); } door_position = 1; }//if cabin at the bottom else //cabin at the top { if (door_position) //if it is opened we need to close it first { door_close_fast(); __delay_ms(200); __delay_ms(200); __delay_ms(200); door_position = 0; led_cabin_moving(); //indicate that we are going down all_motors_off(); }//if door is opened lift_dn(); //lets slow down and fight the gravity do { //empty loop } while (PORTCbits.RC2); //until cabin won't reach the top all_motors_off(); //cabin_position = 0; //cabin at the top led_off(); }//else if cabin at the top }//if long button press else //short button press -> working with the door { if (!door_position) { led_door_opened(); door_open(); __delay_ms(200); __delay_ms(200); if (!PORTCbits.RC1) all_motors_off(); else { __delay_ms(200); all_motors_off(); } door_position = 1; } else { led_off(); door_close_slow(); __delay_ms(200); __delay_ms(200); __delay_ms(200); all_motors_off(); door_position = 0; } } //else, short button press door_button = 0; IOCIE = 1; //SLEEP(); }//door button }//while(1) }//main void __interrupt() isr(void) { if (IOCCF0) { //PORTCbits.RC3 = !PORTCbits.RC3; door_button = 1; //IOCIE = 0; //disable all other buttons/sensors IOCCF0 = 0; IOCIE = 0; } } void all_motors_off(void) { //let the pcb pullup to work TRISC3 = 1; TRISC6 = 1; TRISC7 = 1; TRISB7 = 1; PWM3CON = 0; //disable PWM3 T2CON = 0; //prescaler to 1, timer disabled } void led_off(void) //switching off both leds { PORTBbits.RB5 = 1; PORTBbits.RB6 = 1; } void led_door_opened(void) //turning on led when door is opened { PORTBbits.RB5 = 1; PORTBbits.RB6 = 0; } void led_cabin_moving(void) //other led when cabin is moving { PORTBbits.RB5 = 0; PORTBbits.RB6 = 1; } void door_open(void) //opening the door { RC7PPS = 0x0B; //RC7 as a PWM3 output now TRISB7 = 1; TRISC7 = 1; //briefly disable rc7 output PWM3CON = 0; //disable pwm3 T2PR = 4; //setting pwm period to 50KHz PWM3DCH = 3; PWM3DCL = 192; //lowest duty cycle TMR2IF = 0; T2CLKCON = 0x01; T2CON = 128; //prescaler to 1, timer enabled TRISC7 = 0; PWM3CONbits.EN = 1; //enable PWM } void door_close_slow(void) //closing the door { RB7PPS = 0x0B; //RB7 as a PWM3 output now TRISC7 = 1; TRISB7 = 1; //briefly disable rb7 output PWM3CON = 0; //disable pwm3 T2PR = 4; //setting pwm period to 50KHz PWM3DCH = 3; PWM3DCL = 192; //lowest duty cycle TMR2IF = 0; T2CLKCON = 0x01; T2CON = 128; //prescaler to 1, timer enabledled TRISB7 = 0; PWM3CONbits.EN = 1; //enable PWM } void door_close_fast(void) //closing the door faster - usable for a locking the cabing at the top { RB7PPS = 0x0B; //RB7 as a PWM3 output now TRISC7 = 1; TRISB7 = 1; //briefly disable rb7 output PWM3CON = 0; //disable pwm3 T2PR = 4; //setting pwm period to 50KHz PWM3DCH = 7; PWM3DCL = 192; //lowest duty cycle TMR2IF = 0; T2CLKCON = 0x01; T2CON = 128; //prescaler to 1, timer enabled TRISB7 = 0; PWM3CONbits.EN = 1; //enable PWM } void lift_up(void) //function to move the cabin to the top { led_cabin_moving(); PWM3CON = 0; //disable pwm3 RC3PPS = 0; //no more pwm modulation here TRISB7 = 1; TRISC7 = 1; TRISC3 = 0; PORTCbits.RC3 = 1; //just full power, no pwm needed } void lift_dn(void) //elevator down is basically just "breaking" the cabin to not let the gravity work too much { led_cabin_moving(); TRISB7 = 1; TRISC7 = 1; RC3PPS = 0x0B; //RC3 as a PWM3 output now TRISC3 = 1; //briefly disable rc3 output PWM3CON = 0; //disable pwm3 T2PR = 4; //setting pwm period to 50KHz PWM3DCH = 2; PWM3DCL = 192; //lowest duty cycle TMR2IF = 0; T2CLKCON = 0x01; T2CON = 128; //prescaler to 1, timer enabled TRISC3 = 0; PWM3CONbits.EN = 1; //enable PWM } void door_check(void) { door_position = !PORTCbits.RC1; } void cabin_check (void) { if (!PORTCbits.RC2) cabin_position = 0; else cabin_position = 1; } void delay_1s(void) { __delay_ms(200); __delay_ms(200); __delay_ms(200); __delay_ms(200); __delay_ms(200); }
And some video demonstrations.
As usual all the sources and PCB on my bitbucket (git).