Сумской Анатолий Олександрович

Leson 1: Light control

Lesson 1: Light control


1. General information about Arduino
2. Electricity.
3. Breadboard
4. Resistor
5. LED
6. Button
7. Getting started with S4A

Theoretical part



1. General information about Arduino
At the first lesson we will gain familiarity with the Arduino platform and learn how to use it to create different types of light fixtures.
Platform Arduino is a controller, using which you can collect and process information from sensors, and control a variety of devices: motors, displays, LEDs, etc.
There is a great variety of Arduino models, we will use the platform ArduinoMega 2560.
The exterior of Arduino Mega is shown in Figure where:
             1. USB port for connecting Arduino to a computer and power source 5V.
             2. Connector for an additional power source 9V.
             3. Microcontroller – makes all the computing operations, data collection and processing, controls external devices.
             4. Reset button – Restart. Runs the program over again.
             5. Built-in LED – connected to 13 port.

Arduino Mega 2560

Picture 1 - Arduino Mega 2560


External devices are connected to the numbered ports (pins).


2. Electricity
Everybody use electricity, everyone has a cell phone in his pocket; returning home, we turn on the light, turn on the kettle, microwave, computer, TV. All these devices, as well as the robot, work by electricity. Everyone knows that the current flows through the wires, but how does it come round?
Just to imagine an electric current, imagine a water pipe and the flow of water in it. Then, the current potential is the head of water, current is a jet of water, pipe is a conductor. Then we put a tap on the pipe – when we open the tap, water rushes through the pipe. If the pipe diameter is small, we will see small amount of water. When we soup pipe diameter, the amount of water increases. Such things happen because while the pipe diameter is souping (we open a tap), pipe has less resistance to water head, and the water flows at a higher rate.
Thus, the water flow may be taken as the electric current flowing through the burden. Using this analogy, you probably can independently determine how the current changes when the voltage increases (souping the water pressure in the pipe).
Materials that do not allow current to flow, are called non-conductors, those ones that allow are conductors, materials that allow under certain conditions and do not allow under other conditions are called semiconductors.
In the circuit there are different devices which offer resistance to electric current, for example, conventional bulb – electrons moving under the influence of voltage, pick up speed and gain kinetic energy which they give to the atoms of the crystal lattice spiral of the bulb in the moments of collisions. Atoms are moved oscillatively, internal energy of a substance increases, it appears externally as heating of a conductor and light emission.
Potential and voltage (indicated by the letter U or V) are measured in volts; electric current intensity (indicated by the letter I), or just the current is measured in amperes (A). In electronics they usually use voltage from fractions to tens of volts and the current from the current proportion of milliamperes (mA) to hundreds of milliamperes.
On the nod, the current flows in the direction from plus to minus. By analogy, the water flows from the high pressure area to the empty pipe end (potential difference). When a current flows constantly in one direction, it is called constant.
Point of return / discharge of exhausted charges in a circuit is called Ground (GND). You do not need to understand «land» in the literal sense. It may be the negative pole of the battery or car body, or, indeed, the planet Earth. For convenience, it is consider that Ground is the potential of 0 V. All other potentials are considered to be relative to it.
So, the three values – the current (indicated by the letter I), voltage (U) and resistance (R) – are closely related in a certain ratio. This ratio was found by the famous German physicist Georg Ohm. Therefore, this ratio is called Ohm's law. In simplified form, it looks like this: the current is equal to the voltage divided by the resistance. The higher the voltage resistance and the smaller the resistance is, the larger the current is.
We learnt the basics, in future they will be studied deeply. So, we got to know what are current, voltage, resistance. There is the energy that powers our robot. Now let's describe what the way of the current in a robot is. Current energizes the microcontroller, which produces computational and logical operations using the same small taps, which we discussed above. Taps (logic elements) are connected in sequence (according to the scheme) and are closed or opened depending on the program.
Electrical circuit is often understood as an electronic device consisting of radioactive elements and the board with the conductors which connect these elements. Circuit can also be called a simplified representation of the device on the paper showing how its elements are interconnected.
Thus, the microcontroller through the program controls electrical devices connected to it according to the circuit, such as a lamp or a motor in the robot. In future we will learn how to use the controller for collecting and analyzing environmental parameters, receive actuating signals and use these data to control external devices.


3. Breadboard
We will use a breadboard for setting-up.



Picture 2 - Breadboard


Along the edges two tires are located. They are used for connection power supply and ground. There are two blocks of the main connectors in the center. As can be seen from the figure, connectors of one block are interconnected vertically, 5 connectors.


4. Resistor
We have already told what the resistance is and how it affects the current. But sometimes resistance in the conductor is not enough, and the current that flows is too strong. To limit the current flow, we use additional artificial resistance, which is called a resistor. Resistor reduces the current by converting excess energy into heat.
Resistors have different internal resistance (rated value). To distinguish them color labeling is used. Color, because if resistance was written in numbers, the numbers would be very-very small and could not be identified without magnification. We will use resistors with resistance of 220 ohms, 1 kOhms and 10 kOhms.



Picture 3 - Resistor


5. LED
LED is an electrical component, which is illuminated when electric current passes through it. Its own resistance is very small, so if you need to connect the LED, it is necessary to switch on a resistor included, otherwise the diode can burn. There is a wide variety of LEDs. They come in different emission colors, sizes, shapes, brightness. To determine the direction of current moving, the legs of LEDs have different lengths. Current is supplied to the long leg (anode), ground is connected to the short one (cathode). The variety of LEDs is shown below.



Picture 4 - LED


6. Button
It is often necessary to control robot or other device by cycling. We are going to describe an ordinary computer keyboard. She each key is formed as a tact switch. It is often an open contact that closes instantly when pressed. This type of button has two states "closed" and therefore "open". Button instantly returns to its original position, opening the contact point due to the spring, when loosening downforce (finger). Button has four outputs, they are connected in pairs between themselves. One is connected to the ground, another one – to any other digital output on Arduino.


Tact switches

Picture 5 - Tact switches


7. Getting started with S4A.
For a quick and easy creation of program for Arduino there is an environment of visual programming S4A, based on SKRETCH - visual object-oriented programming environment for teaching students of primary and secondary school. It allows you not to write code, but modularize a program from the blocks. To download the program, please visit s4a.cat or in your personal account.


Look of program S4A

Picture 6 - Look of program S4A


A large field in the middle of the screen is called the domain of the scripts. This is an area in which the program will be collected. The left pane contains all the blocks needed to collect a program. The blocks are divided into 8 functional groups:
- Motion;
- Control;
- Physical appearance;
- Sensors;
- The sound;
- Operators;
- Stylus;
- Variables;
We will consider the basic blocks, which will be used for creating programs for the Arduino.


Group Motion



value of the sensor connected to a specified analog input (can be set from 0 to 1023). Change the input number by clicking on the black triangle next to the name entry.



value of the sensor connected to specified discrete (digital) input. Can take the value 1 and 0 (HIGH and LOW).



transmits to the specified discrete output value HIGH.


digital off  

transmits to the specified discrete output value LOW.




transmits to a specified analog output value from 0 to 255.



Group Control



 block that starts execution of the program by clicking on the green indicator. Placed at the beginning of the program.


forever alone  

inside the block "forever" is a sequence of actions to be performed continuously while the power of board is on.



delay of a specified length.



located inside the block sequence of actions that are executed if the condition is executed.



Group Variables

Variable is an object that has a name and stores the value. Value can be used and modified.



assign the selected variable a value. First you need to create a variable by clicking on “Create a variable” key that is in the elements of the same functional group.


Blocks can be moved to the middle window (Script Pane) by hooking them using a mouse. In this area the program will be formed from the blocks, as from the bricks. It is necessary to pay attention to the fact that the blocks are interconnected like the items in the construction set, it can sometimes be a prompt. If any of the blocks are not connected to each other, that means you are doing something wrong.





1. Blink by LED built in the board
To begin with we write a simple program that lights up the built-in LED connected to the 13 pin.
Let us consider this process step by step:
Step 1. Connect the Arduino to the computer using USB-cable.
Step 2: Pull out blocks to the Script Pane "When clicked on" and "Forever", which normally form a standard structure of the program.





Step 3: To light up the 13th LED put the block «Digital 13 on» into the block “Forever”.




Step 4: To make the LED blink at intervals 1s, it is necessary first to light it up, and then switch off in 1s and wait for 1s before light it up again.

2. To light external LED.
Step 1. Collect circuit on a breadboard. Cathode (short leg) is connected to ground (GND port on the Arduino), the anode – to the plus (a digital port). It will illuminate when voltage on the digital port appears. Place 220 ohm resistor in the circuit. Resistor can be both from the ground and from the plus.




Step 2: To make LED, connected to the 12th output, blink, we change the program port number.


digital wait


3. Employ a light fixture that lights up when the button is held down.
Step 1. Collect the circuit. As both the button and the LED are connected to ground, we remove ground on a common channel for convenience.





Step 2. Reason as follows: if the button is pressed, then we send to 12th port value on, otherwise send value off. Button state is got through the block «sensor Digital2 pressed?» (is sensor connected to the second digital port pressed?). In order to perform certain actions the unit "if" is used under certain condition. If in case of failure conditions it is necessary to perform other actions, use the block "If ... else." Gain a program similar to the following:





4. Employ a light fixture that lights up and goes down when the button is pressed.
Step 1. As this project uses the same elements as in the previous one, the circuit is left unchanged.
Step 2. We put in the variable "LED status", which will be equal to either 0 or 1, and will determine the state of the LED. Its value will be changed by pressing a button. At the beginning of the program execution we will put a variable to 0 (in future we will use the phrase "assign a value to the variable..."). This action should be performed only once, so set the appropriate block on the block "Forever". Next, check the value of the port that is connected to the button. Once the button has been pressed, change the value of the variable "LED status."


when of else


Step 3. In the previous step, we organized changing the value of the variable every time the button is pressed. Now we need to change the LED status, depending on the value of the variable. For this we use the block "If ... else". We also add a small delay before starting the next check.




Введите код: