Making Arduino from scratch. Making your own Arduino Uno Mini What is an Arduino with your own hands

Arduino is a universal platform for DIY microcontrollers. There are many shields (expansion cards) and sensors for it. This diversity allows you to create a number of interesting projects aimed at improving your life and increasing its comfort. The board's areas of application are limitless: automation, security systems, systems for data collection and analysis, etc.

From this article you will learn what interesting things you can do with Arduino. Which projects will be spectacular and which will be useful.

What can you do with Arduino

Robot vacuum cleaner

Cleaning an apartment is a routine task and unattractive, especially since it takes time. You can save it if you delegate part of the housework to a robot. This robot was assembled by an electronics engineer from Sochi - Dmitry Ivanov. Structurally, it turned out to be of sufficient quality and is not inferior in efficiency.

To assemble it you will need:

1. Arduino Pro-mini, or any other similar and suitable in size...

2. USB-TTL adapter if you are using Pro mini. If you chose Arduino Nano, then it is not needed. It is already installed on the board.

3. L298N driver is needed to control and reverse DC motors.

4. Small motors with gearbox and wheels.

5. 6 IR sensors.

6. Engine for turbine (larger).

7. The turbine itself, or rather the impeller from a vacuum cleaner.

8. Motor for brushes (small).

9. 2 collision sensors.

10. 4 x 18650 batteries.

11. 2 DC-DC converters (boost and step-down).

13. Controller for operation (charging and discharging) of batteries.

The control system looks like this:

And here is the power system:

Such cleaners are evolving, factory-made models have complex intelligent algorithms, but you can try to make your own design that will not be inferior in quality to expensive analogues.

Capable of producing a luminous flux of any color, they usually use LEDs in the housing of which there are three crystals glowing in different colors. They are sold to control them; their essence is to regulate the current supplied to each of the colors LED strip, therefore, the intensity of the glow of each of the three colors is adjusted (separately).

You can make your own RGB controller using Arduino, even moreover, this project implements control via Bluetooth.

The photo shows an example of using one RGB LED. To control the tape, you will need an additional 12V power supply, then they will control the gates of field-effect transistors included in the circuit. The gate charging current is limited by 10 kOhm resistors; they are installed between the Arduino pin and the gate, in series with it.

Using a microcontroller you can make a universal remote control remote control controlled from a mobile phone.

For this you will need:

    Arduino of any model;

    IR receiver TSOP1138;

    IR LED;

    Bluetooth module HC-05 or HC-06.

The project can read codes from factory remotes and save their values. After which you can control this homemade product via Bluetooth.

The webcam is installed on a rotating mechanism. It is connected to a computer with installed software. It is based on the computer vision library - OpenCV (Open Source Computer Vision Library), after the program detects a face, the coordinates of its movement are transmitted via a USB cable.

Arduino commands the rotating mechanism drive and positions the camera lens. A pair of servos are used to move the camera.

The video shows how this device works.

Keep an eye on your animals!

The idea is to find out where your animal roams, which can be of interest for scientific research or just for fun. To do this you need to use a GPS tracker. But to store location data on some kind of storage device.

In this case, the dimensions of the device play a decisive role here, since the animal should not feel discomfort from it. To record data, you can use it to work with Micro-SD memory cards.

Below is a diagram of the original version of the device.

The original version of the project used a TinyDuino board and shields for it. If you cannot find one, it is quite possible to use small Arduino copies: mini, micro, nano.

A low-capacity Li-ion element was used for power supply. The small battery lasts about 6 hours. The author ended up fitting everything into a cut-off Tic-Tac jar. It is worth noting that the GPS antenna must point upward to obtain reliable sensor readings.

Code lock burglar

To crack combination locks using Arduino, you will need a servo and stepper motor. This project was developed by hacker Samy Kamkar. This is quite a complex project. The operation of this device is shown in the video, where the author explains all the details.

Of course, such a device is unlikely to be suitable for practical use, but it is an excellent demonstration device.

Arduino in music

This is more likely not a project, but a small demonstration of how this platform has been used by musicians.

Drum machine on Arduino. It is notable for the fact that this is not an ordinary search of recorded samples, but, in principle, sound generation using “hardware” devices.

Parts ratings:

    NPN-type transistor, for example 2n3904 - 1 pc.

    Resistor 1 kOhm (R2, R4, R5) - 3 pcs.

    330 Ohm (R6) - 1 pc.

    10 kOhm (R1) - 1 pc.

    100 kOhm (R3) - 1 pc.

    Electrolytic capacitor 3.3 uF - 1 pc.

For the project to work, you will need to connect the library for fast Fourier series expansion.

This is a fairly simple and interesting “you can show off to your friends” project.

3 robot projects

Robotics is one of the most interesting areas for geeks and just those who like to do something unusual with their own hands, I decided to make a selection of several interesting projects.

BEAM robot on Arduino

To assemble a four-legged walking robot you will need:

    To move the legs you need servomotors, for example, Tower Hobbies TS-53;

    A piece of copper wire of medium thickness (so that it can withstand the weight of the structure and not bend, but not too thick, because it does not make sense);

    Microcontroller - AVR ATMega 8 or Arduino board of any model;

    For the chassis, the design states that a Sintra Frame was used. It's something like plastic, it bends into any shape when heated.

As a result you will get:

It is noteworthy that this robot does not drive, but walks, can step over and climb heights of up to 1 cm.

For some reason, this project reminded me of the robot from the cartoon Wall-e. Its special feature is its use for charging batteries. It moves like a car, on 4 wheels.

Its constituent parts:

    Plastic bottle of suitable size;

  • Mom-dad jumpers;

    Solar panel with an output voltage of 6V;

    As a donor of wheels, engines and other parts - a radio-controlled car;

    Two continuous rotation servos;

    Two conventional servos (180 degrees);

    Holder for AA batteries and for “crown”;

    Collision sensor;

    LEDs, photoresistors, 10 kOhm fixed resistors - 4 pieces in total;

    Diode 1n4001.

Here is the basis - an Arduino board with a proto-shield.

This is what the spare parts from - wheels look like.

The structure is almost assembled, the sensors are installed.

The essence of the robot's work is that it goes to the light. He needs abundance for navigation.

This is more of a CNC machine than a robot, but the project is very entertaining. It is a 2-axis drawing machine. Here is a list of the main components of which it consists:

    (DVD)CD drives - 2 pcs.;

    2 drivers for A498 stepper motors;

    servo drive MG90S;

    Arduino Uno;

    Power supply 12V;

    Ballpoint pen, and other design elements.

The optical disc drive uses blocks with a stepper motor and a guide rod that position the optical head. The motor, shaft and carriage are removed from these blocks.

You will not be able to control a stepper motor without additional equipment, so special driver boards are used; it is better if a motor radiator is installed on them at the time of starting or changing the direction of rotation.

The complete assembly and operation process is shown in this video.

See also 16 best Arduino projects from AlexGyver:

Conclusion

This article covers just a small sampling of all that you can do on this popular platform. In fact, it all depends on your imagination and the task you set for yourself.

Arduino is an amazing electronic platform for creating amateur radio projects. It is easy to use and much cheaper than professional development boards.



But Arduino can be made even cheaper by not purchasing a factory board, but by making it yourself. And this material will tell you how to make an Arduino with your own hands, using components from your local radio store.


Step 1: Components



For Arduino we need:


Microcontroller ATmega 168
Prototyping board (440 or 840 pins)
22 AWG wires
Voltage regulator 7805
Two LEDs
Two 220 Ohm resistors
One 10KΩ resistor
Two 10 µF capacitors
Oscillator 16 MHz
Two 22 pF capacitors
Button
Pin connector
RS232-USB interface converter to 3.3 V

Step 2: organizing the power chain


First of all, we need to supply power to our microcontroller. To do this, we will use a 7805 +5 V voltage regulator. The figure shows the connection diagram.



For the regulator to work correctly, the input voltage must be greater than +5 V, for example, a +9 V battery is suitable as a source. Power is supplied to the red (+) and black (-) squares. This is followed by a 10 µF capacitor. Since it is electrolytic, polarity must be observed. Typically, the anode lead (+) is longer than the cathode lead (-). Also, most capacitors are marked with a stripe on the cathode side. Then there are two wires that “transfer” the power to another part of the board. Next comes the voltage regulator. Here you also need to be careful with the polarity. If you look at it from the front, the left pin will be the input (Vin), the middle one will be the ground (GND), and the right pin will be the output (Vout). We also connect a 10 µF capacitor to the output, observing the polarity.


For greater convenience of using the power line, let’s “throw” it to the left side of the board:



It is also a good idea to add an LED indicator to let you know whether power is currently connected or not. The LED is connected through a 220 Ohm resistor (marked with two red and one brown stripes in the figure). We connect the resistor to the anode (usually a long lead) of the LED. We place the cathode on the ground.



Step 3: Arduino Pins


Now we need to take the microcontroller. In this case, we use Atmel's ATmega 168, but you can also use the ATmega 328. The ATmega 328 runs at the same speed and has the same pin assignments, but it has twice the flash memory and EEPROM memory.


The pin assignments for Arduino can be seen in the following figure:



Step 4: Connecting the Components


Let's consider the connection first with respect to one side of the microcontroller, then the other side. Diagram for pin side 15-28:



Connect ground to pin 22. Then power to pin 21 (analog reference voltage for the ADC) and pin 20 (power for the ADC). On the original Arduino board, the pin for the LED is designated as Pin 13, but in relation to the microcontroller it will be pin 19. Therefore, we connect the anode of the LED to the 19th pin, and connect its cathode to ground through a 220 Ohm resistor. When programming, remember that to light the LED you need to reference pin 13 (Pin 13).


Now let's move on to the opposite side (pins 1-14):



Connect a button to pin 1. This will be used to reset the microcontroller. Before recording a new sketch in Arduino, you will need to press the reset button. A 10 KOhm resistor must be connected to the power supply from the connection of the button to pin 1. And the second contact of the button must be grounded. Pin 8 (GND) must also be grounded, and pin 7 (VCC) must be connected to power. The 16 MHz oscillator is connected to pins 9 and 10. It also leads to 22 pF capacitors to ground.


On the left side of the image there is a programming connector. The functionality of the pins, starting from the leftmost one, is as follows: GND, NC, 5V, TX, RX, NC. NC means not connected anywhere. We connect the RX pin to the second pin of the controller, and the TX pin to the third. Now you can program this homemade Arduino board.


Step 5: Programming



Now we need a USB-TTL interface converter board. The converter can be, for example, FT232RL. After connecting this adapter to the TX and RX pins, as well as the +5V power line, you need to launch the Arduino IDE and select the example (Example sketch files section) Blink from the Digital subsection. Now you need to select the active COM port of the USB adapter, for example, COM1 or COM9. Next, depending on the microcontroller used, you need to select a board (Tools/Board section): Arduino Decimila, Duemilanove or Nano w/ATmega128 or Arduino Duemilanove w/ATmega328.

General information

This version of the Arduino controller, if not the simplest, is certainly the most accessible for self-made. It is based on the already classic Arduino circuit on the ATMega8 controller.

In total, two options have been developed:

  • Modular
  • Single board

Modular option

This controller version consists of three boards:

Single board option

Everything is the same, only on one board:

The board is made of single-sided foil PCB and can be replicated at home using, for example, LUT technology. Board dimensions: 95x62

Microcontroller Programming

After assembling the board, you need to “flash” the controller, load the “bootloader” into it. For this you will need a programmer. We take a clean ATMega8 type controller, install it in the programmer, and connect it to the computer. I used the AVR ISP mkII programmer with the ATMega8-48-88-168 adapter. We program using the Arduino IDE, it will set the necessary fuse bits. The sequence is:

1. Select a programmer (Service > Programmer > AVRISP mkII). If this programmer is used for the first time, you need to install the driver AVRISP-MKII-libusb-drv.zip. If you are using another programmer other than AVRISP mkII, then you need to select the one you need from the list.

2. Selecting a board for the microcontroller (Tools > Board > Arduino NG or older w/ ATmega8). If you are using another microcontroller other than ATmega8, then you need to choose the board that matches it.

3. Record bootloader (Tools > Record bootloader).

4. Install the controller on the board, and that’s it, the Arduino is ready to work.

Microcontrollers are an excellent basis for large quantity devices. In essence, they resemble a computer: permanent memory; RAM; computing core; clock frequency.

Among the many families and types of microcontrollers, beginners often choose AVR Atmega controllers. However, the programming language can seem complicated, so a teacher from Italy decided to develop a simple and convenient board for learning.

Arduino ATmega8 was born, on the basis of which you can assemble a very convenient and simple device.

With these Arduino boards you get a number of advantages:

  • ready divorced printed circuit board with all necessary components and connectors;
  • Atmega microcontrollers;
  • ability to program without programmers – via USB port;
  • power supply from any source 5-20 volts;
  • simple programming language and the ability to use pure C AVR without modifications to the board or firmware.
  • ATmega8 frequency: 0-16 MHz
  • ATmega8 voltage: 5V
  • ATmega8L frequency: 0-8 MHz
  • Frequency ATmega8A: 0-16 MHz

In reality, almost all microcontrollers with an operating voltage of 5 volts operate at a frequency of 16 megahertz if an external quartz resonator is involved. If we take the internal generator, the frequencies will be: 8, 4, 2 and 1 MHz.

Arduino ATmega8 pinout

Below is the pinout for atmega8, which can also be found on the manufacturer’s official website:

Adding ATmega devices

There is one nuance when working with this chip - we need to make some changes to one file so that we can further program the Arduino ATmega8 microcontrollers.

Make the following changes to the file hardware/arduino/boards.txt:

Atmega8o.name=ATmega8 (optiboot 16MHz ext) atmega8o.upload.protocol=arduino atmega8o.upload.maximum_size=7680 atmega8o.upload.speed=115200 atmega8o.bootloader.low_fuses=0xbf atmega8o.bootloader.high_fuses=0xdc atmega 8o.bootloader.path =optiboot50 atmega8o.bootloader.file=optiboot_atmega8.hex atmega8o.bootloader.unlock_bits=0x3F atmega8o.bootloader.lock_bits=0x0F atmega8o.build.mcu=atmega8 atmega8o.build.f_cpu=16000000L atmega8o.build.core=arduino :arduino atmega8o. build.variant=arduino:standard ########################################### ################### a8_8MHz.name=ATmega8 (optiboot 8 MHz int) a8_8MHz.upload.protocol=arduino a8_8MHz.upload.maximum_size=7680 a8_8MHz.upload.speed= 115200 a8_8MHz.bootloader.low_fuses=0xa4 a8_8MHz.bootloader.high_fuses=0xdc a8_8MHz.bootloader.path=optiboot a8_8MHz.bootloader.file=a8_8MHz_a4_dc.hex a8_8MHz.build.mcu=atmega8 a8_8MHz.build. f_cpu=8000000L a8_8MHz.build.core =arduino a8_8MHz.build.variant=standard ############################################ ##################### a8_1MHz.name=ATmega8 (optiboot 1 MHz int) a8_1MHz.upload.protocol=arduino a8_1MHz.upload.maximum_size=7680 a8_1MHz.upload. speed=9600 a8_1MHz.bootloader.low_fuses=0xa1 a8_1MHz.bootloader.high_fuses=0xdc a8_1MHz.bootloader.path=optiboot a8_1MHz.bootloader.file=a8_1MHz_a1_dc.hex a8_1MHz.build.mcu=atmega8 a8_1MHz.build.f _cpu=1000000L a8_1MHz.build .core=arduino a8_1MHz.build.variant=standard ######################################## ######################### a8noboot_8MHz.name=ATmega8 (no boot 8 MHz int) a8noboot_8MHz.upload.maximum_size=8192 a8noboot_8MHz.bootloader.low_fuses=0xa4 a8noboot_8MHz .bootloader.high_fuses=0xdc a8noboot_8MHz.build.mcu=atmega8 a8noboot_8MHz.build.f_cpu=8000000L a8noboot_8MHz.build.core=arduino a8noboot_8MHz.build.variant=standard

So if we go to the menu Service → Fee, then we will see the devices:

  • ATmega8 (optiboot 16MHz ext)
  • ATmega8 (optiboot 8 MHz int)
  • ATmega8 (optiboot 1 MHz int)
  • ATmega8 (no boot 8 MHz int)

Arduino boards

Arduino is sold in many variants; The main thing that unites the boards is the concept of the finished product. You do not need to etch the board and solder all its components; you get a ready-to-use product. You can assemble any device without using a soldering iron. All connections in the basic version are made using a breadboard and jumpers.

The heart of the board is the AVR family microcontroller. Initially, the atmega8 microcontroller was used, but its capabilities are not unlimited, and the board was subject to modernization and changes. The standard board that is most common among hobbyists is the UNO version, there are many variations of it, and its size is comparable to a credit card.

The board is a complete analogue of its larger brother, but in a much smaller size; the arduino atmega168 version was the most popular and inexpensive, but it was replaced by another model - arduino atmega328, the cost of which is similar, but more capabilities.

The next important part is the printed circuit board. Wired and sealed at the factory, it avoids problems with its creation, etching and soldering. The quality of the board depends on the manufacturer of the particular instance, but, in general, it is at a high level. The board is powered using a pair of linear stabilizers, such as L7805, or other LDO voltage stabilizers.

A terminal block is a great way to make a reliable plug connection and quickly make changes to the circuitry of your prototype devices. For those who do not have enough standard connectors, there are larger and more powerful boards, for example, the atmega2560, which has fifty ports available for working with peripherals.

The photo shows the board. Based on it, you can assemble a rather complex robot, a smart home system, or a 3D printer using Arduino.

You should not think that the younger versions are weak, for example, the atmega328 microcontroller, on which the Uno, nano, mini and other models are built, has twice as much memory compared to the 168 model - 2 KB of RAM and 32 KB of Flash memory. This allows you to record more complex programs into the microcontroller memory.

Projects based on Arduino ATmega

A microcontroller in modern electronics is the basis for any device, from a simple LED flasher to universal measuring instruments and even production automation equipment.

Example 1

You can make a tester with 11 functions on an atmega32 microcontroller.

The device has extremely simple diagram, which uses just over a dozen parts. However, you get a fully functional device that can be used to take measurements. Here is a short list of its capabilities:

  1. Circuit continuity test with the ability to measure the voltage drop across the diode junction.
  2. Ohmmeter.
  3. Capacitance meter.
  4. Capacitor resistance or ESR measurement.
  5. Definition of inductance.
  6. Possibility of counting pulses.
  7. Frequency measurement - useful in diagnostics, for example, to check the PWM of the power supply.
  8. The pulse generator is also useful in repairs.
  9. The logic analyzer will allow you to view the contents of bursts of digital signals.
  10. Zener diode tester.

Example 2

It will be useful for radio amateurs to have high-quality equipment, but the station is expensive. It is possible to assemble a soldering station with your own hands; for this you need an Arduino board containing an atmega328 microcontroller.

Example 3

For advanced radio amateurs, it is possible to assemble a more than budget oscilloscope. We will publish this lesson in future articles.

To do this you will need:

  1. Arduino uno or atmega
  2. TFT display 5 inches.
  3. A small set of strapping.

Or its simplified analogue on the Nano board and display from Nokia 5110.

Such an oscilloscope probe will be useful for an auto electrician and electronic equipment repairman.

Example 4

It happens that the controlled modules are remote from each other or the capabilities of one Arduino are not enough - then you can assemble an entire microcontroller system. To ensure communication between two microcontrollers, it is worth using the RS 485 standard.

The photo shows an example of the implementation of such a system and data entry from the keyboard.

Color music on the Arduino ATmega8 microcontroller

For a school disco, you can assemble a DMU with 6 channels.

Transistors VT1-VT6 need to be selected taking into account the power of your LEDs. These are power components - they are needed because the power of the microcontroller is not enough to run powerful lamps or LEDs.

If you want to switch mains voltage and assemble color music using incandescent lamps, you need to install triacs and a driver instead. Add each CMU channel with the following design:

DIY Arduino

Atmega2560 is a powerful and advanced controller, but it’s easier and faster to assemble your first board based on atmega8 or 168.

The left side of the diagram is a USB communication module, in other words, a USB-UART/TTL converter. It, together with the harness, can be removed from the circuit to save space, assembled on a separate board and connected only for firmware. It is needed to convert signal levels.

DA1 is the L7805 voltage regulator. As a basis, you can use a whole range of avr chips that you will find, for example, series, arduino atmega32 or assemble an arduino atmega16. To do this, you need to use different bootloaders, but for each of the MKs you need to find your own.

You can do it even simpler and assemble everything on a solderless breadboard, as shown here, using the 328th Atmega as an example.

Microcontrollers are easy and fun - you can make a lot of cool and interesting things or even become a great inventor without any education or knowledge of low-level languages. Arduino is a step into electronics from scratch, which allows you to move on to serious projects and study complex languages, type C avr and others.

In this article, I decided to put together a complete step-by-step guide for Arduino beginners. We will look at what Arduino is, what you need to start learning, where to download and how to install and configure the programming environment, how it works and how to use the programming language, and much more that is necessary to create full-fledged complex devices based on the family of these microcontrollers.

Here I will try to give a condensed minimum so that you understand the principles of working with Arduino. For a more complete immersion in the world of programmable microcontrollers, pay attention to other sections and articles of this site. I will leave links to other materials on this site for a more detailed study of some aspects.

What is Arduino and what is it for?

Arduino is an electronic construction kit that allows anyone to create a variety of electro-mechanical devices. Arduino consists of software and hardware. The software part includes a development environment (a program for writing and debugging firmware), many ready-made and convenient libraries, and a simplified programming language. The hardware includes a large line of microcontrollers and ready-made modules for them. Thanks to this, working with Arduino is very easy!

With the help of Arduino you can learn programming, electrical engineering and mechanics. But this is not just an educational constructor. Based on it, you can make really useful devices.
Starting from simple flashing lights, weather stations, automation systems and ending with smart home systems, CNC machines and unmanned aerial vehicles. The possibilities are not even limited by your imagination, because there are a huge number of instructions and ideas for implementation.

Arduino Starter Kit

In order to start learning Arduino, you need to acquire the microcontroller board itself and additional parts. Best to buy starter kit Arduino, but you can choose everything you need yourself. I recommend choosing a set because it's easier and often cheaper. Here are links to the best sets and individual parts that you will definitely need to study:

Basic Arduino kit for beginners:Buy
Large set for training and first projects:Buy
Set of additional sensors and modules:Buy
Arduino Uno is the most basic and convenient model from the line:Buy
Solderless breadboard for easy learning and prototyping:Buy
Set of wires with convenient connectors:Buy
LED set:Buy
Resistor kit:Buy
Buttons:Buy
Potentiometers:Buy

Arduino IDE development environment

To write, debug and download firmware, you need to download and install the Arduino IDE. This is a very simple and convenient program. On my website I have already described the process of downloading, installing and configuring the development environment. So here I will just leave links to latest version programs and

Version Windows Mac OS X Linux
1.8.2

Arduino programming language

When you have a microcontroller board in your hands and a development environment installed on your computer, you can start writing your first sketches (firmware). To do this, you need to become familiar with the programming language.

Arduino programming uses a simplified version of the C++ language with predefined functions. As in other C-like programming languages, there are a number of rules for writing code. Here are the most basic ones:

  • Each instruction must be followed by a semicolon (;)
  • Before declaring a function, you must specify the data type returned by the function, or void if the function does not return a value.
  • It is also necessary to indicate the data type before declaring a variable.
  • Comments are designated: // Inline and /* block */

You can learn more about data types, functions, variables, operators and language constructs on the page on You do not need to memorize and remember all this information. You can always go to the reference book and look at the syntax of a particular function.

All Arduino firmware must contain at least 2 functions. These are setup() and loop().

setup function

In order for everything to work, we need to write a sketch. Let's make the LED light up after pressing the button, and go out after the next press. Here's our first sketch:

// variables with pins of connected devices int switchPin = 8; int ledPin = 11; // variables to store the state of the button and LED boolean lastButton = LOW; boolean currentButton = LOW; boolean ledOn = false; void setup() ( pinMode(switchPin, INPUT); pinMode(ledPin, OUTPUT); ) // function for debouncing boolean debounse(boolean last) ( boolean current = digitalRead(switchPin); if(last != current) ( delay (5); current = digitalRead(switchPin); ) return current ) void loop() ( currentButton = debounse(lastButton); if(lastButton == LOW && currentButton == HIGH) ( ledOn = !ledOn; ) lastButton = currentButton ; digitalWrite(ledPin, ledOn);

// variables with pins of connected devices

int switchPin = 8 ;

int ledPin = 11 ;

// variables to store the state of the button and LED

boolean lastButton = LOW ;

boolean currentButton = LOW ;

boolean ledOn = false ;

void setup() (

pinMode(switchPin, INPUT);

pinMode(ledPin, OUTPUT);

// function for debouncing

boolean debounse (boolean last ) (

boolean current = digitalRead(switchPin);

if (last != current ) (

delay(5);

current = digitalRead(switchPin);

return current ;

void loop() (

currentButton = debounse(lastButton);

if (lastButton == LOW && currentButton == HIGH ) (

ledOn = ! ledOn;

lastButton = currentButton ;

digitalWrite(ledPin, ledOn);

In this sketch I created additional function debounse to suppress contact bounce. There is information about contact bounce on my website. Be sure to check out this material.

PWM Arduino

Pulse width modulation (PWM) is the process of controlling voltage using the duty cycle of a signal. That is, using PWM we can smoothly control the load. For example, you can smoothly change the brightness of an LED, but this change in brightness is obtained not by decreasing the voltage, but by increasing the intervals of the low signal. The operating principle of PWM is shown in this diagram:

When we apply PWM to an LED, it starts to quickly light up and go out. The human eye is not able to see this because the frequency is too high. But when shooting video, you will most likely see moments when the LED is not lit. This will happen provided that the camera frame rate is not a multiple of the PWM frequency.

Arduino has a built-in pulse width modulator. You can use PWM only on those pins that are supported by the microcontroller. For example, Arduino Uno and Nano have 6 PWM pins: these are pins D3, D5, D6, D9, D10 and D11. The pins may differ on other boards. You can find a description of the board you are interested in in

To use PWM in Arduino there is a function. It takes as arguments the pin number and the PWM value from 0 to 255. 0 is 0% fill with a high signal, and 255 is 100%. Let's write a simple sketch as an example. Let's make the LED light up smoothly, wait one second and fade out just as smoothly, and so on ad infinitum. Here is an example of using this function:

// The LED is connected to pin 11 int ledPin = 11; void setup() ( pinMode(ledPin, OUTPUT); ) void loop() ( for (int i = 0; i< 255; i++) { analogWrite(ledPin, i); delay(5); } delay(1000); for (int i = 255; i >0; i--) ( analogWrite(ledPin, i); delay(5); ) )

// LED connected to pin 11

int ledPin = 11 ;

void setup() (

pinMode(ledPin, OUTPUT);

void loop() (

for (int i = 0 ; i< 255 ; i ++ ) {

analogWrite(ledPin, i);

delay(5);

delay(1000);

for (int i = 255; i > 0; i -- ) (