Monday, May 18, 2020

SwitchBot DIY using LPSTK-CC1352R , SG90 servo motor, and ProjectZero to away from COVID-19.

Due to COVID-19, people don't want to touch anything such as light switch arbitrarily and it is any super easy to DIY light switch robot to allow you to turn on or off light through BLE of you SmartPhone instead of touching the switch directly.

The follow steps show you how to DIY SwitchBot using Texas Instruments LPSTK-CC1352R, SG90 servo motor, and ProjectZero.

1. Add CONFIG_PWM_0 and use DIO22 as PWM output pin in project_zero.syscfg



2. Add "#include < ti/drivers/PWM.h >" and the following global variables in project_zero.c to use PWM related APIs later.

/* Period and duty in microseconds */
int16_t   pwmPeriod = 20000;
int16_t   duty = 1500;
int16_t   dutyInc = 500;

int16_t i=0;

/* Sleep time in microseconds */
PWM_Handle pwm1 = NULL;
PWM_Params params;

3. Add the following PZ_SERVO_EVT, servoClock, and Servo_clockHandler to send PZ_SERVO_EVT.

#define PZ_SERVO_EVT         11
static Clock_Struct servoClock;static void Servo_clockHandler(UArg arg);

static void Servo_clockHandler(UArg arg)
{
    //PWM_setDuty(pwm1, duty);
    pzButtonState_t *pButtonState = ICall_malloc(sizeof(pzButtonState_t));
            if(pButtonState != NULL)
            {
                //*pButtonState = buttonMsg;
                if(ProjectZero_enqueueMsg(PZ_SERVO_EVT, pButtonState) != SUCCESS)
                {
                  ICall_free(pButtonState);
                }
            }
}

4. Add "case PZ_SERVO_EVT:" in ProjectZero_processApplicationMessage to put SG90 back to original position after writing ON/OFF characteristics

      case PZ_SERVO_EVT:
      {
          PWM_setDuty(pwm1, duty);
          break;
      }


5.  Init/start servoClock and PWM in ProjectZero_init.

    Util_constructClock(&servoClock, Servo_clockHandler, 300, 0, false, 0);
    Util_startClock(&servoClock);

    PWM_init();
    PWM_Params_init(&params);
    params.dutyUnits = PWM_DUTY_US;
    params.dutyValue = 0;
    params.periodUnits = PWM_PERIOD_US;
    params.periodValue = pwmPeriod;
    pwm1 = PWM_open(CONFIG_PWM_0, &params);
    if (pwm1 == NULL) {
        /* CONFIG_PWM_0 did not open */
        while (1);
    }

    PWM_start(pwm1);
    PWM_setDuty(pwm1, duty);

6. In "case LS_LED0_ID:..." section of ProjectZero_LedService_ValueChangeHandler. Add the following code to set PWM to rotate SG90 servo motor according to ON/OFF characteristics writing.

        if (pCharData->data[0]!=0){
            PWM_setDuty(pwm1, duty-500);
            Util_startClock(&servoClock);
        }
        else{
            PWM_setDuty(pwm1, duty+800);
            Util_startClock(&servoClock);
        }


7. Build and download firmware into LPSTK-CC1352R

8. Connect GND/3V3/DIO22 pins on LPSTK-CC1352R to BROWN(GND)/RED(PWR)/ORANGE(PWM) on SG90.

9. Power on LPSTK-CC1352R and use TI SimpleLink Starter App to connect to Project Zero to toggle ON/OFF.






No comments:

Post a Comment