
We create a robot at home

Surely, after watching movies about robots, you often wanted to build your comrade, but you did not know where to start. Of course, you will not be able to build a two-legged terminator, but we do not strive for this. A simple robot can be assembled by anyone who knows how to hold a soldering iron correctly in their hands and this does not require deep knowledge, although they will not hurt. Amateur robotics is not much different from circuitry, only much more interesting, because such areas as mechanics and programming are also affected. All components are easily accessible and not so expensive. So progress does not stand still, and we will use it to our advantage.
Introduction
So. What is a robot? In most cases, this is an automatic device that responds to any environmental action. Robots can be controlled by humans or perform pre-programmed actions. Typically, a robot has a variety of sensors (distance, rotation angle, acceleration), video cameras, manipulators. The electronic part of the robot consists of a microcontroller (MK) - a microcircuit that encloses a processor, a clock generator, various peripherals, operational and read-only memory. In the world there is a huge variety of microcontrollers for different applications and based on them you can assemble powerful robots. For amateur buildings, AVR microcontrollers are widely used. They are, by far, the most affordable and on the Internet you can find many examples based on these MK. To work with microcontrollers you need to be able to program in assembler or C and have a basic knowledge of digital and analog electronics. In our project we will use C. Programming for MK is not much different from programming on a computer, the syntax of the language is the same, most functions are practically no different, and new ones are quite easy to learn and convenient to use.
What we need
To begin with, our robot will be able to simply go around obstacles, that is, repeat the normal behavior of most animals in nature. All that we need to build such a robot can be found in radio stores. Decide how our robot will move. I consider the most successful caterpillars used in tanks to be the most convenient solution, because the caterpillars have greater cross-country ability than the wheels of the car and it is more convenient to control them (for turning it is enough to rotate the caterpillars in different directions). Therefore, you will need any toy tank in which the tracks rotate independently of each other, this can be bought at any toy store at a reasonable price. From this tank you only need a platform with tracks and motors with gears, the rest you can safely unscrew and throw. We also need a microcontroller, my choice fell on the ATmega16 - it has enough ports for connecting sensors and peripherals and in general it is quite convenient. You also need to purchase some radio components, a soldering iron, a multimeter.
We make a fee with MK

Robot circuit
In our case, the microcontroller will perform the functions of the brain, but we will start not with it, but with the power of the robot brain. Proper nutrition is the key to health, so we will start with how to properly feed our robot, because novice robot builders usually make mistakes on this. And in order for our robot to work properly, you need to use a voltage stabilizer. I prefer the L7805 chip - it is designed to output a stable voltage of 5V at the output, which our microcontroller needs. But due to the fact that the voltage drop on this chip is about 2.5 V, a minimum of 7.5 V must be supplied to it. Along with this stabilizer, electrolytic capacitors are used to smooth out voltage ripples and the diode must be included in the circuit to protect against reverse polarity.
Now we can take care of our microcontroller. The case of MK - DIP (it is more convenient to solder) and has forty conclusions. On board there is an ADC, PWM, USART and much more, which we will not use yet. Consider several important nodes. The RESET pin (9th foot of the MK) is pulled up by the resistor R1 to the "plus" of the power supply - this must be done! Otherwise, your MK may be unintentionally dumped or, more simply, buggy. It is also a desirable measure, but not required, to connect a RESET through a ceramic capacitor C1 to ground. In the diagram you can also see an electrolyte at 1000 microfarads, it saves from voltage dips during the operation of the motors, which will also favorably affect the operation of the microcontroller. The quartz crystal X1 and the capacitors C2, C3 should be located as close as possible to the terminals XTAL1 and XTAL2.
I will not talk about how to flash MK, since this can be read on the Internet. We will write the program in C, as the programming environment I chose CodeVisionAVR. This is a fairly convenient environment and is useful for beginners, because it has a built-in wizard for creating code.

My robot board
Engine management
An equally important component in our robot is the engine driver, which makes it easier for us to manage it. Never and under no circumstances should motors be connected directly to MK! In general, powerful loads cannot be controlled directly from the microcontroller, otherwise it will burn. Use key transistors. For our case, there is a special chip - L293D. In such simple projects, always try to use this particular chip with the “D” index, as it has built-in diodes to protect against overloads. This chip is very easy to control and easy to get in electronic stores. It is available in two DIP and SOIC packages. We will use it in the DIP case due to the convenience of mounting on the board. L293D has separate motor and logic power. Therefore, we will power the microcircuit itself from the stabilizer (VSS input), and motors directly from batteries (VS input). L293D can withstand a load of 600 mA per channel, and it has two of these channels, that is, two motors can be connected to the same chip. But in order to play it safe, we will combine the channels, and then it will take one micra for each engine. It follows that the L293D can withstand 1.2 A. To achieve this, you need to combine the legs of the micra, as shown in the diagram. The microcircuit works as follows: when a logical “0” is supplied to IN1 and IN2, and a logical unit to IN3 and IN4, the engine rotates in one direction, and if the signals are inverted, a logical zero is applied, then the motor will start to rotate in the other direction. Conclusions EN1 and EN2 are responsible for the inclusion of each channel. We connect them and connect to the "plus" power from the stabilizer. Since the chip heats up during operation, and the installation of radiators is problematic for this type of enclosure, the heat is removed by the GND legs - it is better to solder them over a wide contact area. That's all you need to know about engine drivers for the first time.
Obstacle sensors
So that our robot can navigate and not crash into everything, we will install two infrared sensors on it. The simplest sensor consists of an IR diode, which emits in the infrared spectrum and a phototransistor, which will receive a signal from the IR diode. The principle is this: when there is no obstacle in front of the sensor, then the infrared rays do not fall on the phototransistor and it does not open. If there is an obstacle in front of the sensor, then the rays from it are reflected and fall on the transistor - it opens and current flows. The disadvantage of such sensors is that they can respond differently to different surfaces and are not protected from interference - the sensor can accidentally work from extraneous signals from other devices. Modulation of the signal can protect against interference, but for now we will not bother with this. To start, and that's enough.

The first version of my robot sensors
Robot firmware
To revive the robot, you need to write firmware for it, that is, a program that would take readings from the sensors and control the engines. My program is the simplest, it does not contain complex structures and everyone will understand. The following two lines include the header files for our microcontroller and the commands for generating delays: The following lines are conditional, because the PORTC values depend on how you connected the engine driver to your microcontroller: The value 0xFF means that the output will be a log. “1” and 0x00 is the log. "0". With the following construction, we check whether there is an obstacle in front of the robot and from which side:
#include
#include
PORTC.0 = 1;
PORTC.1 = 0;
PORTC.2 = 1;
PORTC.3 = 0;
if (!(PINB & (1<
{
...
}
If light from the IR diode gets on the phototransistor, then a log is set on the foot of the microcontroller. "0" and the robot starts moving backward to move away from the obstacle, then turns around so that it does not collide with the obstacle again and then goes forward again. Since we have two sensors, we check for the presence of an obstacle twice - on the right and on the left, and therefore we can find out which side the obstacle is from. The delay_ms (1000) command indicates that it will take one second before the next command starts to execute.
Conclusion
I looked at most of the aspects that will help you assemble your first robot. But this is not the end of robotics. If you collect this robot, then you will have a lot of opportunities for its expansion. You can improve the algorithm of the robot, such as what to do if the obstacle is not from some side, but right in front of the robot. It also does not hurt to install the encoder - a simple device that will help you accurately position and know the location of your robot in space. For clarity, you can install a color or monochrome display, which can show useful information - the battery charge level, distance to obstacles, various debugging information. Improving sensors - TSOP installation (these are IR receivers, which receive a signal only of a certain frequency) instead of ordinary phototransistors. In addition to infrared sensors, there are ultrasonic ones, they are more expensive, and they are also not without drawbacks, but recently they are gaining popularity among robot engineers. In order for the robot to respond to sound, it would be nice to install microphones with an amplifier. But really interesting, I think, is installing a camera and programming based on it machine vision. There is a set of special OpenCV libraries with which you can program face recognition, movement along colored beacons, and many other interesting things. It all depends on your imagination and skills. so that the robot can respond to sound, it would be nice to install microphones with an amplifier. But really interesting, I think, is installing a camera and programming based on it machine vision. There is a set of special OpenCV libraries with which you can program face recognition, movement along colored beacons, and many other interesting things. It all depends on your imagination and skills. so that the robot can respond to sound, it would be nice to install microphones with an amplifier. But really interesting, I think, is installing a camera and programming based on it machine vision. There is a set of special OpenCV libraries with which you can program face recognition, movement along colored beacons, and many other interesting things. It all depends on your imagination and skills.
Component List:
- ATmega16 in DIP-40 package>
- L7805 in TO-220
- L293D in DIP-16 х2 pcs.
- 0.25 W resistors with ratings: 10 kΩ x1 pcs., 220 Ohm x4 pcs.
- ceramic capacitors: 0.1 μF, 1 μF, 22 pF
- electrolytic capacitors: 1000 microfarads x 16 V, 220 microfarads x 16V x2 pcs.
- diode 1N4001 or 1N4004
- 16 MHz quartz crystal
- IR diodes: any two are suitable.
- phototransistors, also any, but reacting only to the wavelength of infrared rays
Firmware code:
/*****************************************************
Прошивка для робота
Тип МК : ATmega16
Тактовая частота : 16,000000 MHz
Если у тебя частота кварца другая, то это нужно указать в настройках среды:
Project -> Configure -> Закладка "C Compiler"
*****************************************************/
#include
#include
void main(void)
{
//Настраиваем порты на вход
//Через эти порты мы получаем сигналы от датчиков
DDRB=0x00;
//Включаем подтягивающие резисторы
PORTB=0xFF;
//Настраиваем порты на выход
//Через эти порты мы управляем двигателями
DDRC=0xFF;
//Главный цикл программы. Здесь мы считываем значения с датчиков
//и управляем двигателями
while (1)
{
//Едем вперёд
PORTC.0 = 1;
PORTC.1 = 0;
PORTC.2 = 1;
PORTC.3 = 0;
if (!(PINB & (1<
{
//Едем назад 1 секунду
PORTC.0 = 0;
PORTC.1 = 1;
PORTC.2 = 0;
PORTC.3 = 1;
delay_ms(1000);
//Заворачиваем
PORTC.0 = 1;
PORTC.1 = 0;
PORTC.2 = 0;
PORTC.3 = 1;
delay_ms(1000);
}
if (!(PINB & (1<
{
//Едем назад 1 секунду
PORTC.0 = 0;
PORTC.1 = 1;
PORTC.2 = 0;
PORTC.3 = 1;
delay_ms(1000);
//Заворачиваем
PORTC.0 = 0;
PORTC.1 = 1;
PORTC.2 = 1;
PORTC.3 = 0;
delay_ms(1000);
}
};
}
About my robot
At the moment, my robot is almost complete.

It has a wireless camera, a distance sensor (both the camera and this sensor are mounted on a turret), an obstacle sensor, an encoder, a remote control signal receiver and an RS-232 interface for connecting to a computer. It works in two modes: stand-alone and manual (receives control signals from the remote control), the camera can also be turned on / off remotely or by the robot itself to save battery power. I am writing firmware to protect the apartment (transferring an image to a computer, detecting movements, detouring a room).
I upload the video according to my wishes:
UPD I reloaded the photos and made small corrections in the text.
The article was published by me in the journal "Hacker" for August 2009.