Smart Home Applications realized with Scratch

Bachelor Thesis 2017 37 Pages

Computer Science - Programming


Table of Contents

1 Introduction and Motivation

2 Concept and technical Requirements
2.1 Concept
2.2 Hardware Concept
2.3 Software Concept

3 Technical Demands

4 Implementation
4.1 Hardware Construction on the Breadboard
4.1.1 Communication Atmel – Raspberry Pi
4.1.2 Transmission of Radio Signals
4.1.3 Comparison of Radio Receivers
4.2 Prototyp
4.2.1 Printed Circuit Board
4.2.2 Testing Prototyp
4.3 Software Implementation
4.3.1 Implementation on the Rasperry Pi and Usage with Scratch
4.3.2 Implementation on the ATmega328P

5 Application Scenario

6 Summary


Schon lange spielen Smarthome-Anwendungen eine große Rolle in unseren Haushalten. Überwachung von jedem Ort durch Webcams, Einfahren der Markise bei Sonnenuntergang oder Beleuchtung durch Bewegung - die vielseitige Einsetzbarkeit und die flexible Anpassung ist im Normalfall nur teureren Systemen vorbehalten. Diese Arbeit hat sich zum Ziel gesetzt ein Funkboard zu entwickeln, welches durch eine Anlernfunktion die Möglichkeit bietet alle Geräte im 433 MHz Funkbereich einzubinden mit Ausnahme von Modulen, welche einen rolling code oder hopping code verwenden. Weiters wurde eine Software Schnittstelle zur Programmiersprache Scratch hergestellt, mit der bereits Kinder und Programmieranfänger einfache Smarthome-Anwendungen ohne Vorkenntnisse programmieren können. Neben den Hard- und Softwarevoraussetzungen werden in dieser Arbeit der Aufbau der Schaltung auf einem Steckbrett, der Test des Prototypen und die Implementierung der Software beschrieben. Den Abschluss bildet die Programmierung einer einfachen Smarthome - Anwendung mit der Programmiersprache Scratch.

Schlagwörter: Smarthome, Funkboard, Raspberry Pi, Scratch


For many years, smart home applications have played a major role in our households. Real-time monitoring everywhere via the web, retracting the awning at sunset or lighting tasks by movement: the versatility and flexible adaptation is normally reserved for more expensive systems. The aim of this thesis is to develop a radio board that offers the possibility to integrate all devices in the frequency range of 433 MHz due to a learning function except modules using rolling code or hoping code. Furthermore, a software interface to the programming language Scratch was developed, with which children and programming beginners can already program simple smart home applications without prior knowledge. In addition to the hardware and software requirements, the design of the circuit on a breadboard, the test of the prototype and the implementation of the software are described in this thesis. Finally, a simple smart home scenario will be programmed with the visual programming language Scratch.

Keywords: smart home, radio board, Raspberry Pi, Scratch

1 Introduction and Motivation

Lighting, heating, roller shutters and many electrical appliances - from home and from anywhere, the so-called smart home is increasingly coming into our households. Based on a number of reasons such as saving time or energy or feeling safer, intelligent living makes life easier. Depending on the needs numerous wireless systems are offered. Via radio, Bluetooth or WLAN, they all share in common that with little installation effort modules can also be complemented, depending on the manufacturer. However, this flexible extensibility is not possible in the lower price range. Especially in the radio frequency range of 433 MHz, there are cheap sockets, lamp holders, heating thermostats and many more devices that can be controlled by radio. In a lower price range, these devices have to be combined from many manufacturers which makes an interaction difficult. Accordingly, this work aims to develop a system with which exactly this requirement is covered, whereby it should be possible to use devices from different manufacturers with a single system. In addition to time-controlled tasks, sensor-controlled switching should also be possible. This could be the activation of the air conditioning system when a certain temperature value is exceeded, or the triggering of an alarm due to the detection of movements. With a visual programming interface, the installation should be possible for non-professionals, while learning a programming language for children should be possible by using an object-oriented programming language. In the first part of this thesis, the necessary technical requirements are considered. Since the extensibility and individual programming of the system is in the focus, the credit card-sized computer Raspberry Pi is used in combination with an Atmel microcontroller, which allows a flexible adaptation to the respective home scenario. In the next part, the implementation of the hardware is first explained. Through building the circuit on a breadboard, the basic functions are tested for feasibility and then subsequently tested on a prototype. Thereafter, the software is implemented to fulfill all programming requirements. In the last section, a possible user scenario is simulated, whereby radio modules are taught and integrated into the system. Subsequently, time-controlled and sensor-controlled tasks can be performed. The main purpose of this project is to achieve flexible and individual extensibility and ease of use. More detailed information on similar learning boards based on the Raspbotics system is available on http://www.raspbotics.at [1].

2 Concept and technical Requirements

In the following chapter, a brief concept of the work will be outlined and the necessary technical requirements for the implementation of this project will be considered.

2.1 Concept

Depending on the manufacturer, smart home systems available on the market are linked to their components. This means that one has to know in advance which components - such as smoke detectors, garage door openers, etc. - are compatible. A system update with additional modules at a later date is only possible if the desired devices are exclusively available from this manufacturer. Furthermore, a combination of devices with several transmission protocols such as radio, WLAN and Bluetooth is only available for expensive systems. This means that updating at a later time to adapt a system to the current standard is almost impossible with the exception of expensive systems. Another very important point is that a configuration of various system components or a software integration of different sensors in a smart home system is difficult for laymen.

The concept of this work is to create a system through which it is possible to combine radio components in the frequency range of 433 MHz, even if these have different manufacturers. This ensures that a later upgrade of system modules is still possible. Another important approach is the easy handling of the smart home system. Laymen and even children should be able to control lamps, radio sockets, heaters and many more with software called Scratch, which is designed for children learning a programming language. Since Raspbotics already has a system that makes it possible for beginners to start learning a programming language using Scratch, the aim of this work is to develop another board for this system called radioboard. The requirement of this board is the possibility to implement common devices in the smart home area in the frequency range of 433 MHz regardless of their manufacturer. This includes sockets, lamps, heaters, garage doors, door gongs and many more devices (see Figure 1).

illustration not visible in this excerpt

Figure 1: Concept of a smart home application controlled by the radio board and programmed in Scratch [2], [3]

2.2 Hardware Concept

In order to implement the previously-presented concept, a Raspberry Pi is necessary, which is the interface between Scratch and the radio board. On this board, the widely-used microcontroller ATmega328P from the company Atmel is placed, which was acquired by Microchip in 2016. This microcontroller controls the radio receiver and radio transmitter and processes values of optional sensors for temperature, light and many more. Because the Raspberry Pi has to handle the communication with the microcontroller and the visual programming interface Scratch, which needs many resources, the RPi 2 Model B upwards has to be used.

The UART bus serves as a connection between the radio board and the Raspberry Pi and the I2C protocol for reading and storing the radio protocols on the EEPROM 24LC256. The advantage of the I2C bus is that additional components can be added at any time. Only the pull-up resistors values ​​have to be changed if there more devices or longer wires are used for connecting several boards. Because the GPIOs of the Raspberry Pi are only 3.3 V compatible and the ATmega328P is a 5 V microcontroller, a level shifter will be needed for converting these two voltage levels for the SDA and SCL line of the I2C bus.

The RJ45 socket - which is common for the Raspbotics system - provides among the RX and TX lines of the UART bus the ground, 3.3 V and 5 V lines which supply most of the sensors available on the market. Due to the flexible RJ45 cable, reverse polarity protected connection of the components is ensured especially for children. On the radio interface one digital pin is used each for the transmitter and receiver. One of the two available hardware interrupt pins (INT0) is used for the receiver. This offers the advantage that it is possible to work with interrupt routines, while valuable time resources are not given away by constant polling. Furthermore, the common 6-pin ICSP (Integrated Circuit Serial Programming) interface for Atmel boards is available, which uses the SPI protocol and can be used for programming directly on the board using standard programming devices available on the market without having to remove the microcontroller from the board. In addition, a further UART and I2C interface is to be installed on the board as a socket, which allows the integration of additional devices or the terminal output.

The EEPROM 24LC256 is used to store the learned radio protocols. With a memory size of 256 kBit, it offers sufficient memory space to control many different radio modules. In addition, the memory chip can be used for holding important user data such as temperature curves, times of garage door openings and much more. Buttons and a RGB light-emitting diode are used to configure and control the function of the radio board. Furthermore, a small prototype area (2.54 mm grid) with power supply bus is available. This means that a wide range of sensors can be integrated to control the radio modules.

2.3 Software Concept

Several variants are used on the software side. The Raspbian image is used for the Raspberry Pi, which is available as an operating system on an SD card. On this image, the graphic programming interface for children - called Scratch - is installed, which was developed by the MIT (Massachusetts Institute of Technology) in 2007 and helps children and beginners to learn a programming language.

The software interface to Scratch is a Python script, which must be extended by the radio connection. For this purpose, the data from the radio sensors is received via UART interface and transmitted to Scratch via the script. In the Scratch IDE (integrated development environment), the Scratch programming language can be used to read the transferred data via the block sensor value (see Figure 2).

illustration not visible in this excerpt

Figure 2: Reading received data in Scratch

In the other direction, the data exchange takes place similarly. The data to be sent to the respective radio module is received via the same Python script and is evaluated and transmitted via UART bus to the microcontroller. The ATmega328P reads the corresponding protocol from the EEPROM and sends it via the radio transmitter. The programming language C is used for the ATmega328P placed on the radio board. If the radio code cannot be learned (because the radio transmitter is lost), the known code can also be entered manually into a text file. This text file is stored on the Raspberry Pi and will be transferred to the EEPROM as required. In order to examine the radio protocol, it should be poosible to visualize it, in any browser by a graph.

3 Technical Demands

The main requirement for the system is to create a simple possibility to implement a smart home scenario that is also feasible for laymen. In this case, it should be possible to use low-cost radio modules in the frequency range of 433 MHz independent of the manufacturer. As a result, a large number of devices can be combined, which has previously been reserved for more expensive smart home systems. Usually the protocols of the devices are not known, especially because many manufacturers use a proprietary code. Therefore, a simple method must be available to read the protocol for laymen without an oscilloscope or logic analyzer. The built-in radio receiver - which is normally used to communicate with sensors or other devices - serves as a training interface. A script running in the background reads the code of the associated remote control or radio device and stores it on an EEPROM. In addition, it should be possible to enter a known protocol manually into a file, whereby the module can be re-used even if the remote control is lost. In order to compare protocols of different devices

It should be possible to display the radio signals with a graph.

As a user scenario, it should be possible to install various radio modules such as gongs, fire detectors, radio buttons, sockets and many more in one’s house or apartment. In the programming language Scratch - which is installed by default on the image of the Raspberry Pi - it should be possible to detect sensor values value as shown in Figure 2. By detecting a certain value users should be able to send a command for controlling radio sockets, garage doors, awnings and many more (see Figure 3).

illustration not visible in this excerpt

Figure 3: Broadcast comment for controlling devices

Accordingly, the garage door, lighting or roller shutters can also be controlled. Furthermore radio sensors, such as temperature sensors or smoke detectors to control the climate at home or warn the owner in the event of a fire, should also be used. The safety aspect is covered by radio window contacts or radio motion detectors. To enable realistic time-controlled tasks, Scratch must be extended by time and date blocks. The possibility of integrating manufacturer-independent devices results in a wide range of applications for a smart home, which can be extended at any time. For advanced users, a prototype area should be available on the radio board for the integration of additional sensors.

4 Implementation

At the beginning, this chapter deals with the hardware setup on a breadboard, which is recommended before the development of the prototype to check the components and the basic structure of the software for feasibility. Due to disturbances such as interference, walls and windows the possible distance between radio modules is usually far below the manufacturer's specifications. Therefore, a more powerful receiver module is used in the second part and tested for higher ranges. In the final part of this chapter, the software has to be realized. To design a software interface for the communication between Raspberry Pi and Scratch, a Python script has to be used and on the radio board the microcontroller that has to be programmed in C handles with reading and writing radio protocols, the storage on the EEPROM, the realizing of the training function and sending values for the graph that can be displayed in any browser. An ICSP interface - placed on the radio board - allows software to be changed at any time.

4.1 Hardware Construction on the Breadboard

The 8-bit microcontroller ATmega328P is the main part of the radio board. With its 23 IO pins, a 10bit Analog Digital Converter (ADC) and a UART, I2C and SPI bus system, it offers all requirements for this project. Although an internal oscillator can be used for the clocking, a 16 MHz crystal has been used for this requirement to guarantee a consistent clock rate, even at faster clock rates of different bus transmissions. To realize a basic circuit for using the ATmega328P with an external quartz and an ICSP socket to program the microcontroller on the board, a minimal circuit has to be used, as shown in the Figure 4.

illustration not visible in this excerpt

Figure 4: Minimal circuit of the ATmega328P by using a quartz [4]

In addition to a 10 kOhm pull-up resistor at the reset pin, two 22 pf capacitors at the quartz pins - which are intended to help the quartz to oscillate - and another 100 nF blocking capacitor - to prevent voltage dips - must be placed close to the supply pins. In order to program the microcontroller on the board without having to remove it every time, a 6pin ICSP socket is installed. For this socket, attention was paid to use the common ICSP pin assignment of all microcontrollers from Atmel to provide compatibility with all programming devices available on the market. The SPI protocol is used for this ICSP programming, which means that, in addition to the voltage supply, one need four further pins (MOSI, MISO, SCLK, SS). These pins can also be used for other tasks during operation. The MySmartUSBlight programming device - which already provides the power supply during programming - was used for this project (see Figure 5 (I)).

Since the radio board can be used as a standalone product, an external power supply is required. A mini-USB socket was chosen, which provides the 5 V of a conventional charger. Owing to the size, this socket can ideally be soldered by hand, which is also a great advantage for the available assembly kit variant. Due to the implementation of a second UART socket - with a UART to TTL converter as shown in Figure 5 (C) - messages or values of variables can be sent to the terminal, which is very helpful in the programming phase. For the usage with Scratch, the power supply is provided by the Raspberry Pi (see Figure 5 (A)). Further digital and analog pins as well as PWM pins for the control of servos and motors are available in the form of a prototype area, which can be used for custom DIY applications as with conventional development boards.

The following Figure 5 shows the complete hardware construction on the breadboard. The most important components are listed in Table 1.

illustration not visible in this excerpt

Table 1: Hardware components on the breadboard

illustration not visible in this excerpt

Figure 5: Complete hardware construction on the breadboard

4.1.1 Communication Atmel – Raspberry Pi

A communication via I2C bus was originally planned, although the EEPROM used for data storage on the radio board is already addressed as an I2C slave device. Normally this bus system is suitable for up to 127 devices. Because the Raspberry Pi only supports the master mode and the ATmega328P has to be used as a slave, the UART interface was used for the connection between the Raspberry Pi and the microcontroller. As is usual for the Raspbotics system, a RJ45 socket is used on both sides, which also allows children to connect the modules to one another in a polarity reversal protected manner. Via this cable, the Raspberry Pi provides a 3.3 V, 5 V and ground line as a voltage supply via the Raspbotics baseboard. With these two voltage levels, different modules available on the market can be used without having to use a voltage divider or level shifter. For the UART bus a RX (receive) and TX (transmit) line is available on the GPIO header, which must be crossed. Since the pins of the Raspberry Pi are only 3.3 V compatible, the 5 V RX line has to be reduced to 3.3 V, which can be realized with a level shifter, a transistor circuit or a simple voltage divider as shown in Figure 5 (G). The amount of data that can be sent with this transfer rate depends on the protocol used. In this case the common data size of 10bits has been used. The first bit serves as start bit and is a logical low bit and the last bit serves as stop bit and is logical high. This means that 10 bits have to be transmitted for one byte (see Figure 6).

illustration not visible in this excerpt

Figure 6: UART data size [5]



ISBN (eBook)
ISBN (Book)
File size
2.5 MB
Catalog Number
Institution / College
University of Applied Sciences Technikum Vienna
smart home radio board Raspberry Pi Scratch




Title: Smart Home Applications realized with Scratch