Skip to main content

Learning Mikroe Clicker 2 - Project 2: Automate your Home

In the second project of our Learning Mikroe Clicker 2 series get to grips with the Motion, Relay, Buzz and UART Click modules as you learn how to build a basic home automation application.

Parts list

Qty Product Part number
1 MikroElektronika Clicker 2 for STM32 MIKROE-1685 8829048
1 MikroElektronika USB UART Click USB to UART Add On Board, FT232RL - MIKROE-1203 8828913
1 MikroElektronika Relay Click - MIKROE-1370 8209858
1 MikroElektronika BUZZ Click Buzzer Add On Board - MIKROE-945 9235961
1 MikroElektronika Motion Click - PIR500BMotion Sensor Add On Board - MIKROE-1589 8829008
1 MikroElektronika mikroBUS expansion shield - MIKROE-1154 7916485

Welcome to Automate your Home with MikroElektronika Clicker 2 for STM32. This is the second project in our learning with MikroElektronika series - from beginner to advanced - these projects will help you get started with embedded development for the first time, or if you are an experienced engineer, to familiarise yourself with the Mikroe development environment.

You will learn:

  • Necto studio development environment
  • Writing a microSDK application in Embedded C
  • Integrating external libraries into your application
  • Extending the capabilities of your Clicker 2 with add-on Click modules

In this series:

Home Automation Index

Part 0: Preparation

Part 1: Building a basic home automation device

Part 2: Extending the project

Part 0: Preparation

0.1 Download and Install NectoStudio

First, ensure you have downloaded and installed NectoStudio, the MikroElektronika cross-platform IDE. A free 90-day trial is available for evaluation to allow you to get started.

0.2 Hardware

To complete this project you will require a MikroElektronika Clicker 2 for STM32, the mikroBUS shield as well as four MikroE 'Clicks' which are an easy way to add additional functionality to the Clicker 2.

  • Clicker 2 for STM32 - MIKROE-1685
  • MikroBUS shield - MIKROE-1154
  • Motion Click - MIKROE-1589
  • USB UART Click - MIKROE-1203 - provides a UART interface via a Micro USB connection. This will allow you to communicate between the Clicker 2 and your PC/Mac
  • Relay Click - MIKROE-1370
  • Buzz Click - MIKROE-945

0.3 Assembling the Hardware

  • First, we need to assemble the Home Automation device, we’ll start by inserting Motion Click to mikroBUS1 and USB UART Click to mikroBUS 2 socket on the Clicker 2 development board.

shield_1-dc_cd3e65f28a4e5d6eb4aa356c6ab53bed000e6ef7.jpg

  • On the MikroBUS Shield – we will insert Relay Click on mikroBUS 1 and Buzz Click on mikroBUS 2 socket. Then, connect the MikroBUS Shield to the Clicker 2 development board.

shield_2-dc_49182554be3509601c5e486847a4ccd0d7ca77c8.jpg

Part 1: Building the motion detector

1.1 Setting up NectoStudio Project

After installing the NectoStudio and registering for your trial license, we need to open NectoStudio and setup the project.

  • Click on New Project.
  • Enter the project name and choose where the project will be saved.
  • Select the appropriate project template - for this example, we will choose the following settings:
    • language - mikroC
    • template - mikroSDK Application
    • project type – Application

create_new_project_b51c43318df9669ea4f03e7fb8248975b93d1139.png

  • Select an appropriate profile – for now, Clicker 2 for STM32 doesn’t have its own hardware profile in NectoStudio, but that will be added in future updates. For now, we can use any Fusion for ARM / STM32 v8 profile, because they are based on the STM32F407 MCUlect the Fusion for ARM v8 profile and then click on Next and in the next window on Finish.

create_new_project-set_profile_2be9f1255be6ad7b671d3eb8cc03aa2cd7131e56.png

Upon creating the new project, we will see the default template for creating the mikroSDK 2.0 programs.

1.2 Update Board Configuration File - board.h for MikroBUS shield

We will use both mikroBUS sockets for this example, so we need to change the board.h file and add the pins that are connected to mikroBUS 1 and 2 sockets.

  • Navigate to board.h - y>ou can change this file in NectoStudiowhen the default template is displayed – in main.c file, on the line: #include “board.h” Right-click on board.h file and choose option Goto definition or F2 to open the board.h file in a new window.
  • Comment the lines mikroBUS1, mikroBUS2,mikroBUS3 and mikroBUS 4 and copy the lines from the board.h we supplied with this project.

This change of the board.h use the pinout of the Clicker 2 development board on the mikroBUS sockets – both on Clicker 2 and MikroBUS Shield.

mikrobusmapping_26b47170b803aeb2e5f93088f62ea337a3b923e1.png

mikrobusmapping2_ddc8d2a8151eb496f04c97efe80111c8da6ec9f3.png

1.3 Download and configure libraries - Motion, Relay, Log

Before we can use the Temp&Hum 4 Click board, we need to download the library from the Packages menu.

  • Download Libraries - Click on the Packages menu and click on Browse tab – enter the package name, when the library is displayed click on Install. After the library installs – it will be displayed in the Installed tab. From there you can access the various information such as library version, description of the most used functions and example of the code.
  • Configuring Libraries - after you install the library, it will become an available option in the Dependencies tree in the Project Manager window. Access the library manager by right-clicking on Dependencies and selection option Manage project libraries, which will open the Library Manager.

2dependencies_3cafe30753473a23aa85e47e59cacc01603ca116.png

  • Select the required libraries Motion and Relay from Click group and Log from MikroSDK group, Click OK and wait for NectoStudio to reconfigure the project.

2libraries_56cc07c98d83c1fc022278b41ce1cc6a4e7c69c0.png

  • There’s one library that’s still not included in NectoStudio – Sound library, but we will use the Legacy driver library. To include this library in the project – we first need to copy the Sound.h and Sound.emcl files to the root folder of the project. Then – in Project manager window, double click on the memake.txt file to open it and add the following lines :
binaries : {
sound.emcl
}

headers: {
sound.h
}
  • Save and close the file.

With the project's dependencies configured, now we can get to building the application.

1.4 Includes and context structures

  • The first thing we need to do is ensure the header files for the libraries we require are included.

#include “board.h”
#include “log.h”
#include “motion.h”
#include “sound.h”
#include “relay.h”
  • Create context structure objects for motion, relay and logger and two more for motion detection state. The code should look like the following :
static motion_t motion;
static relay_t relay;
static log_t logger;
motion_detect_state_t motion_state;
motion_detect_state_t motion_old_state;

 

1.5 Initialise drivers

  • Within the application_init() function in main.c, we need to initialize the driver objects and map the drivers to the pins on which the Click modules are connected.

In the Application Init section of the code – we will configure the Click Boards by initializing them to appropriate mikroBUS socket and assigning the default configuration values.

Log will use PD9 and PD8 pins, these pins correspond to the pins on the UART pins on the mikroBUS 2 socket of the Clicker 2 – we will define them with log_cfg.rx/tx_pin.

Motion Click will be configured on mikroBUS socket 1.

Relay Click is on mikroBUS 3 socket ( which is mikroBUS socket 1 on the MikroBUS Shield, but in the board.h is defined as mikroBUS 3 )

Buzz Click uses the B8 pin from mikroBUS 4 socket ( mikroBUS 2 on Shield ).

We will use the SoundInit function to configure the B8 pin.

The Application Init section should look like this::

  log_cfg_t log_cfg;
  motion_cfg_t cfg;
  relay_cfg_t cfg_r;
  
  // Log initialization
  // Starts with 2 second delay
  
  Delay_ms( 2000 );
  
  log_cfg.level = LOG_LEVEL_DEBUG;
  log_cfg.rx_pin = PD9;
  log_cfg.tx_pin = PD8;
  LOG_MAP_USB_UART( log_cfg );
  log_init( &logger, &log_cfg );
  log_printf( &logger, "Application Init\r\n");
  Delay_ms( 300 );
  
  // Motion Click initialization and config
  
  motion_cfg_setup( &cfg );
  MOTION_MAP_MIKROBUS( cfg, MIKROBUS_1 );
  motion_init( &motion, &cfg );
  log_printf( &logger, "Motion Click initialized!\r\n");
  Delay_ms( 300 );
  motion_default_cfg( &motion );
  log_printf( &logger, "Motion sensor enabled!\r\n");
  Delay_ms( 300 );
  
  // Relay Click initialization and config
  
  relay_cfg_setup( &cfg_r );
  RELAY_MAP_MIKROBUS( cfg_r, MIKROBUS_3 );
  relay_init( &relay, &cfg_r );
  log_printf(&logger, "Relay Click initialized\r\n");
  relay_default_cfg( &relay );
  Delay_ms( 500 );
  
  motion_state = MOTION_NO_DETECT;
  motion_old_state = MOTION_DETECTED;
  
  // Sound init
  
  Sound_Init(&GPIOB_ODR, 8);
  Delay_ms( 1000 );
  buzz_play(1000, 65);
  Delay_ms( 300 );
  buzz_play(1000, 65);
  Delay_ms( 300 );
  buzz_play(1000, 65);
  Delay_ms( 300 );

1.6 Write the application

Finally, we need to add our application code within application_task(), this code is run and looped continuously after application_init() has been run.

In the Application Task, we will check the motion state and for every change assign appropriate action.

If the new movement is detected, we will display the message in PC terminal, play the Tone on the Buzz Click and turn ON the Relay Click.

void application_task()
{
  // Task implementation
  motion_state = motion_get_detected( &motion );
  
  if( motion_state == MOTION_DETECTED && motion_old_state == MOTION_NO_DETECT )
  {
    log_printf( &logger, "Movement detected\r\n");
    motion_old_state = MOTION_DETECTED;
    buzz_play( 1000, 65 );
    relay_set_state( &relay, 1, RELAY_STATE_ON );
    log_printf( &logger, "Relay turned on!\r\n");
  }
  
  if( motion_old_state == MOTION_DETECTED & motion_state == MOTION_NO_DETECT )
  {
    log_printf( &logger, "Standby\r\n");
    motion_old_state = MOTION_NO_DETECT;
    relay_set_state( &relay, 1, RELAY_STATE_OFF );
    log_printf( &logger, "Relay is off!\r\n");
  }
}

Your code is now complete!

1.7 Configuring the clock

Next, we must configure the timing source for the STM32 microcontroller on the Clicker 2 - this is often referred to as the clock.

  • Navigate to MCU settings - Click on the spanner_icon_3e3dfa175bab78292505a08c8fb0559b54bac285.png icon, choose MCU Settings and click on the Load configuration from scheme template.

mcu_settings_c71853dd45fb33ab45b6e611883820f248954f4d.png

  • Load the appropriate clock configuration scheme - Select: >Referent Clock : 16; System clock:168; Tags: 168MHz internal and press Apply.

referent_clock_1889074f88b0e71d7a23fe631fd7fe89684ec60a.png

1.8 Building the application

We now need to build the code - i.e turn the code into a binary file which is understood by the STM32 microcontroller.

  • Click the Hammer icon in the main menu bar to start building your project.

start-debug_7a1d952525d8389de1b7342ec0aaa6bca040d176.png

If the project doesn’t have any errors in code, it will build successfully and upon completing the build it will display the following message:

2build_b12a96972db69c24f47062ca28e26ecfafb521ca.png

1.9 Programming Clicker 2

Finally, we will program the application onto the Clicker 2 development board through the bootloader – here’s how I do it.

  • Right-click on the main.c file in the Project Manager window and choose Open file location.

homeautomationlocate1_8378644b6ddce634d9cc2e182a557b934acf010c.png

  • Go one folder above and find the build-HomeAutomation folder

homeautomationlocate_eb9e92465f1ad32b8df9b96cd4c2609fcc068c5b.png

  • In the HomeAutomation folder, you’ll find the HomeAutomation.hex file, which we’ll program

homeautomationhex_b4b132c5e9c3cc708618ee6c17327be110957149.png

  • Now let’s open the HID bootloader from Tools menu

bootloader_dd125909b01c571122cd459c6c82f95675f1dc8b.png

  • Connect the USB cable to the development board and turn the Clicker 2 ON.

You’ll see the name of the development board and in the next 5 seconds, it will be connected to the bootloader. Press Reset on the dev board to connect it again if needed.

  • Drag the HomeAutomation.hex file to the HID Bootloader and you will initialize programming.

bootloader2_fad4cc946d67854c0dcd6e628b22b7464e100b37.png

uploading_hex_8ffa286d3607d4ec685146c57ae0e0a0db975442.png

After the programming finishes and board restarts, after 5 seconds the board will turn on.

In next couple of seconds, you’ll hear 3 tones that signalize that board is initialized successfully.

1.95 Testing the application

When you connect the USB cable to USB UART Click – you can connect to the appropriate COM port in UART terminal and check the status of the Relay click.

  • Connect an additional cable to the USB UART Click and open the UART Bootloader.
  • Select the appropriate COM port and enter the baud rate chosen earlier - 9600bps.

If you have been successful the output on the terminal should look like this:

homeautomationtest_1a7d79b76c93c4b43b8130f4b05ab5af91e59a0c.png

After movement is detected – the Relay Click will turn on Relay 1. You can connect the terminal connectors to a simple circuit ( let’s say – a light source or led blink circuit ) and switch it ON/OFF each time movement is detected.

homeautomationfinal_c7f3dd7ce499610860f0a10b1a36ca5b29335980.png

Part 2: Extending the Project

Now that you have successfully built our example project - get creative! Take a look around you and see how you can put your new skills to use with your own project - perhaps you could build a motion-activated battery-powered night light, an automatic hand sanitiser dispenser or a pet food dispenser that dispenses only when your pet is near and at the correct time of day.

Share your project with us on Designspark when it is completed!

MikroElektronika (MIKROE) is committed to changing the embedded electronics industry through the use of industry-standard hardware and software solutions. In 2011, the company invented the mikroBUS™ development socket standard and the compact Click Boards that use the standard to dramatically cut development time. Now the company offers 1300+ Click Boards. MikroElektronika also makes the world’s widest range of compilers and provides development environments, development boards.
DesignSpark Electrical Logolinkedin