• We are creating an Android application to control a home robot via Bluetooth. Six Easy Ways to Connect Arduino to Android

    With Wi-Fi module.

    On Arduino Uno WiFi is provided for everything comfortable work with microcontroller: 14 digital inputs/outputs (6 of them can be used as PWM outputs), 6 analog inputs, USB connector, power connector, ICSP connector and microcontroller reset button.

    The highlight of the board is the ESP8266 WiFi module, which allows Arduino to exchange information with other modules via wireless networks 802.11 b/g/n standards.

    ESP8266 allows you to flash an Arduino board without using a USB cable OTA mode(Firmware Over The Air - “firmware over the air”).

    Video review of the board

    Connection and setup

    To get started with the Arduino Uno WiFi board, operating system Windows download and install the Arduino IDE on your computer - Arduino IDE.

    Did something go wrong?

    Setting up the WiFi module

    Arduino firmware via WiFi

    Arduino Uno WiFi has one more in stock nice bonus- the ability to upload sketches without using a USB cable in OTA (Firmware Over The Air) mode. Let's take a closer look at how to do this.


    To do this you need to enter the menu: Tools Port and select the desired port.

    Since we are flashing the Arduino via WiFi, the board will be identified as a remote device with an IP address

    The environment is configured, the board is connected. You can proceed to uploading the sketch. Arduino IDE contains a large list ready-made examples, in which you can see the solution to a problem. Let's choose among the examples of LED blinking - the “Blink” sketch.
    Flash the board by clicking on the program download icon.
    After booting, the LED will start flashing once per second. This means that everything worked out.

    Now you can move on to examples of use.

    Examples of use

    Web server

    Let's set up a simple web server that will display a page with the current values ​​of the analog inputs.

    web-server.ino /* An example of a simple web server running on Arduino Uno WiFi. The server displays the values ​​on the analog inputs and updates the information every two seconds. Contact the server at http:// /arduino/webserver/ Please note: the example only works with the Arduino Uno WiFi Developer Edition. */#include #include void setup() ( Wifi.begin () ; Wifi.println ( "Web Server is up" ) ; // Display a message about the server start in the wifi console) void loop() ( while (Wifi.available () ) ( process(Wifi) ; ) delay(50 ) ; ) void process(WifiData client) ( String command = client.readStringUntil ("/" ) ; if (command = = "webserver" ) ( WebServer(client) ; ) ) void WebServer(WifiData client) ( client.println("HTTP/1.1 200 OK" ) ; client.println("Content-Type: text/html" ) ; client. println ("Connection: close" ) ; client.println ( "Refresh: 2" ) ; // Header that specifies the page refresh period in seconds client.println(); client.println(" " ) ; // Form the page client.println( " UNO WIFI Web-server " ) ; client.print( "

    Example of outputting values ​​from analog pins

    "
    ) ; client.print("
      " ); for (int analogChannel = 0 ; analogChannel< 4 ; analogChannel++ ) { int sensorReading = analogRead(analogChannel) ; client.print ("
    • on the analog input") ; client.print(analogChannel) ; client.print(": " ) ; client.print (sensorReading) ; client.print ("
    • " ) ; ) client.println ( "
    " ) ; client.print (DELIMITER) ; // Don't forget to close the connection! }

    Board elements

    Microcontroller ATmega328P

    The heart of the Arduino Uno WiFi platform is the 8-bit microcontroller of the AVR family - ATmega328P.

    Microcontroller ATmega16U2

    The ATmega16U2 microcontroller provides communication between the ATmega328P microcontroller and the USB port of the computer. When connected to a PC, the Arduino Uno WiFi is detected as a virtual COM port. The firmware of the 16U2 chip uses standard drivers USB-COM, so no external driver installation required.

    Power pins

      VIN: Voltage from external power supply (not related to 5V from USB or other regulated voltage). Through this pin you can both supply external power and consume current if an external adapter is connected to the device.

      5V: The pin receives a voltage of 5 V from the board's stabilizer. This stabilizer provides power to the ATmega328 microcontroller. It is not recommended to power the device through the 5V pin - in this case, a voltage stabilizer is not used, which can lead to board failure.

      3.3V: 3.3 V from the board stabilizer. The maximum output current is 1 A.

      GND: Conclusions of the earth.

      IOREF: The pin provides expansion boards with information about the operating voltage of the microcontroller. Depending on the voltage, the expansion board can switch to the appropriate power supply or use level converters, allowing it to work with both 5V and 3.3V devices.

    I/O Ports

      Digital inputs/outputs: pins 0 – 13
      The logical level of one is 5 V, zero is 0 V. The maximum output current is 40 mA. Pull-up resistors are connected to the contacts, which are disabled by default, but can be enabled by software.

      PWM: pins 3, 5, 6, 9, 10 and 11
      Allows you to output 8-bit analog values ​​as a PWM signal.

      ADC: pins A0 – A5
      6 analog inputs, each of which can represent analog voltage as a 10-bit number (1024 values). The ADC capacity is 10 bits.

      TWI/I²C: SDA and SCL pins
      For communication with peripherals using a synchronous protocol, via 2 wires. To work, use the Wire library.

      SPI: pins 10(SS), 11(MOSI), 12(MISO), 13(SCK).
      Communication via the SPI interface is carried out through these pins. To work, use the SPI library.

      UART: pins 0(RX) and 1(TX)
      These pins are connected to the corresponding pins of the ATmega16U2 microcontroller, which acts as a USB-UART converter. Used to communicate between the Arduino board and a computer or other devices via the Serial class.

    LED indication

    USB Type-B connector

    Connector USB Type-B designed for flashing the Arduino Uno WiFi platform using a computer.

    External power connector

    Connector for connecting external power from 7 V to 12 V.

    5V voltage regulator

    When the board is connected to an external power source, the voltage passes through the MPM3610 regulator. The stabilizer output is connected to the 5V pin. The maximum output current is 1A.

    3.3V voltage regulator

    Stabilizer MPM3810GQB-33 with 3.3 volt output. Provides power to the ESP8266 WiFi module and is output to the 3.3V pin. The maximum output current is 1A.

    ICSP connector for ATmega328P

    The ICSP connector is designed for in-circuit programming of the ATmega328P microcontroller. Using the SPI library, these pins can communicate with expansion boards via the SPI interface. The SPI lines are routed to a 6-pin connector, and are also duplicated on digital pins 10(SS), 11(MOSI), 12(MISO) and 13(SCK).

    ICSP connector for ATmega16U2

    The ICSP connector is designed for in-circuit programming of the ATmega16U2 microcontroller.

    The ESP8266 chip is one of the most popular tools for organizing wireless communications in projects smart home. Using a wireless controller, you can organize communication via the WiFi interface, providing Arduino projects Internet access and the possibility of remote control and data collection. Such popular boards as WeMos and NodeMcu, as well as a huge number of homemade projects, have been created based on the ESP8266. In this article, we will find out what the ESP82266 is, what its varieties are, how to work with the ESP8266 in the Arduino IDE.

    ESP8266 is a microcontroller with a WiFi interface that has the ability to execute programs from flash memory. The device was released in 2014 Chinese company Espressif and almost immediately became popular.

    The controller is inexpensive, has a small number of external elements and has the following technical parameters:

    • Supports Wi-Fi protocols 802.11 b/g/n with WEP, WPA, WPA2;
    • Has 14 input and output ports, SPI, I2C, UART, 10-bit ADC;
    • Supports external memory up to 16 MB;
    • Required power supply is from 2.2 to 3.6 V, current consumption is up to 300 mA, depending on the selected mode.

    An important feature is the absence of user non-volatile memory on the chip. The program is executed from an external SPI ROM using dynamic loading of the necessary program elements. Access to the internal peripherals can be obtained not from the documentation, but from the API of a set of libraries. The manufacturer indicates the approximate amount of RAM - 50 kB.

    Features of the ESP8266 board:

    • Convenient connection to a computer – via USB cable, food from it;
    • Availability of built-in 3.3V voltage converter;
    • Availability of 4 MB flash memory;
    • Built-in buttons for rebooting and flashing;
    • All ports are routed onto the board using two combs with a pitch of 2.5 mm.

    Areas of application of the ESP8266 module

    • Automation;
    • Various systems for a smart home: Wireless control, wireless sockets, temperature control, addition to alarm systems;
    • Mobile electronics;
    • Tag ID;
    • Children's toys;
    • Mesh networks.

    esp8266 pinout

    There are a huge number of varieties of the ESP8266 module. The figure shows some of them. The most popular option is ESP 01.

    The execution of the program must be determined by the state of the GPIO0, GPIO2 and GPIO15 ports when the power supply ends. Two important modes can be distinguished - when the code is executed from the UART (GPIO0 = 0, GPIO2 = 1 and GPIO15 = 0) for flashing a flash card and when it is executed from an external ROM (GPIO0 = 1, GPIO2 = 1 and GPIO15 = 0) in the standard mode.

    The pinout for ESP01 is shown in the picture.

    Contact description:

    • 1 – ground, 8 – power. According to the documentation, the voltage is supplied up to 3.6 V - this is important to take into account when working with Arduino, which is usually supplied with 5 V.
    • 6 – RST, needed to reboot the microcontroller when a low logic level is applied to it.
    • 4 – CP_PD, also used to put the device into energy saving mode.
    • 7 and 0 – RXD0 and TXD0, this is a hardware UART required for flashing the module.
    • 2 – TXD0, an LED is connected to this pin, which lights up when the logic level on GPIO1 is low and when data is transmitted via UART.
    • 5 – GPIO0, input and output port, also allows you to put the device into programming mode (when the port is connected to a low logic level and voltage is applied).
    • 3 – GPIO2, input and output port.

    ESP-12 pinout

    The main differences between Arduino and ESP8266

    • The ESP8266 has a larger amount of flash memory, while the ESP8266 does not have non-volatile memory;
    • ESP8266 processor is faster than Arduino;
    • ESP8266 has Wi-Fi;
    • ESP8266 consumes more current than Arduino;

    Programming ESP8266 in Arduino IDE

    The esp8266 development kit includes:

    • Compiler from the GNU Compiler Collection.
    • Libraries, WiFi, TCP/IP protocol stacks.
    • A means of loading information into the controller program.
    • Operating IDE.

    Initially, ESP8266 modules are supplied with firmware from the manufacturer. With its help, you can control the module from an external microcontroller and work with Wi-Fi as a modem. There are also many other ready-made firmware. Some of them allow you to configure the module’s operation using a WEB interface.

    Can be programmed from Arduino environment IDE. With its help, you can easily write sketches and upload them to the ESP8266, flash the ESP8266, and do not require the Arduino board itself. Arduino IDE supports all kinds of ESP8266 modules.

    IN present moment The following functions can be implemented for ESP8266:

    • Basic functions of the Wiring language. You can control the GPIO ports in the same way as the pins on the Arduino board: pinMode, digitalRead, digitalWrite, analogWrite. The analogRead(A0) command allows you to read ADC values. Using the analogWrite (pin, value) command, you can connect PWM to the desired GPIO output. When value=0 PWM is disabled, maximum value reaches a constant equal to 1023. Using the attachInterrupt and detachInterrupt functions, you can interrupt on any GPIO port except 16.
    • Timing and delay. Using the millis and micros commands you can return the ms and μs that have passed since the start. Delay allows you to pause program execution for the desired time. Also, the delay(…) function allows you to maintain normal Wi-Fi work, if the sketch contains large elements that take more than 50 ms to execute. Yield() is an analogue of the delay(0) function.
    • Serial and Serial1 (UART0 and UART1). Serial work on ESP8266 is similar to work on Arduino. Writing and reading data blocks code execution if the 128-byte FIFO and 256-byte software buffer are full. The Serial object uses hardware UART0, you can set pins GPIO15 (TX) and GPIO13 (RX) for it instead of GPIO1(TX) and GPIO3(RX). To do this, after the Serial.begin(); you need to call Serial.swap();. Similarly, Serial1 uses UART1, which works for transmission. The required pin for this is GPIO2.
    • Macro PROGMEM. Its operation is similar to that of Arduino. Allows you to move read only data and string constants into flash memory. At the same time, the ESP8266 does not store the same constants, which leads to additional waste of flash memory.
    • I2C. Before working with the I2C bus, the buses are selected using the Wire.pins(int sda, int scl) function.
    • SPI, OneWire – fully supported.

    Using esp8266 to communicate with Arduino over WiFi

    Before connecting to Arduino, it is important to remember that the supply voltage of the ESP8266 cannot be higher than 3.6, while on the Arduino the voltage is 5 V. You need to connect 2 microcontrollers using resistive dividers. Before connecting the module, you need to familiarize yourself with the pinout of the selected ESP8266. The connection diagram for ESP8266-01 is shown in the figure.

    3.3 V from Arduino to Vcc&CH_PD on the ESP8266 module, Ground from Arduino to ground from ESP8266, 0 – TX, 1 – RX.

    For support stable operation ESP8266 needs source DC voltage at 3.3 V and maximum current 250 mA. If the power comes from a USB-TTL converter, malfunctions and malfunctions may occur.

    Working with the Wi-Fi library for the ESP8266 is similar to the library for a regular shield. There are several features:

    • mode(m) – to select one of three modes: client, access point, or both modes at the same time.
    • softAP(ssid) – needed to create an open access point.
    • softAP(ssid, password) – creates an access point with a password, which must consist of at least 8 characters.
    • WiFi.macAddress(mac) and WiFi.softAPmacAddress(mac) – defines the MAC address.
    • WiFi.localIP() and WiFi.softAPIP() – determining the IP address.
    • printDiag(Serial); – will allow you to find out diagnostic data.
    • WiFiUDP – support for sending and receiving multicast packets in client mode.

    The work is performed according to the following algorithm:

    • Connecting USB-TTL to USB and to ESP.
    • Launching Arduino IDE.
    • Select the required port, board, frequency and flash memory size in the tools menu.
    • File - Examples - ESP8266WiFi - WiFiWebServer.
    • Write down the SSID and password of the Wi-Fi network in the sketch.
    • Start compiling and uploading code.
    • Wait for the firmware process to finish, disconnect GPIO0 from ground.
    • Set the speed to 115200.
    • A connection will occur and the IP address will be recorded.
    • Open the browser, enter address bar IP number/gpio/1
    • Look at the port monitor; if an LED is connected to the GPIO2 output, it should light up.

    NodeMCU based on esp8266

    NodeMCU is a platform based on the esp8266 module. Used to control the circuit remotely using the Internet via Wi-Fi. The board is small-sized, compact, inexpensive, and has a USB connector on the front side. Nearby are buttons for debugging and rebooting the microcontroller. An ESP8266 chip is also installed. Supply voltage is from 5 to 12 V, it is advisable to supply more than 10 V.

    The board's big advantage is its low power consumption. They are often used in self-powered circuits. There are only 11 ports on the board general purpose, some of them have special functions:

    • D1 and D2 – for I2C/ TWI interface;
    • D5-D8 - for SPI interface;
    • D9, D10 – for UART;
    • D1-D10 – can work as PWM.

    The platform has a modern API for hardware input and output. This allows you to reduce the number of steps when working with equipment and when setting it up. With the help of NodeMCU firmware, you can use the full operational potential for rapid device development.

    WeMos based on esp8266

    WeMos is another type of platform based on the esp8266 microcontroller. Accordingly, there is a Wi-Fi module, Arduino IDE is supported, and there is a connector for an external antenna. The board has 11 digital inputs/outputs, which (except D0) support interrupt/pwm/I2C/one-wire. The maximum supply voltage reaches 3.3 V. There is also a USB connector on the platform. Analog input 1 with a maximum voltage of 3.2V.

    To work with the module, you need to install the CH340 driver and configure the Arduino IDE for the ESP8266. To do this, you need to add the address http://arduino.esp8266.com/stable/package_esp8266com_index.json in the settings menu in the “additional link for board manager” line.

    After this, you need to find the esp8266 by ESP8266 package and install it. Then you need to select the Wemos D1 R2 microcontroller from the tools menu and write down the desired sketch.

    Conclusions on ESP8266

    With boards based on the ESP8266 chip, you can add capabilities to your projects. big internet”, making them much more intelligent. Remote control, collecting and analyzing data on the server, voice processing and working with images - all this becomes available when we connect our project via WiFi to the Internet. In the following articles, we will take a closer look at how you can program esp8266-based devices, and also pay attention to such popular boards as WeMos and NodeMcu.

    Arduino boards and similar microcontrollers make creativity more accessible than ever, they write. Regardless of the purpose of use - to automate your home or control LED strips, or even to protect your property, these amazing little technical devices are the core of most DIY electronic devices.

    If you need to command your Arduino to change the position of the pin jumpers (for example, turn on the light), then the Arduino will require the user to press physical button or use a sensor. For many projects, using human finger pressure or similar methods to control devices is fine, but what should you do if you just wanted to build a circuit that could be remotely accessed?

    This article gives brief description six ways to connect your Android device to any compatible Arduino board.

    1. ArduinoDroid allows you to create sketches

    The first device on our list is ArduinoDroid. This application works via USB On The Go (OTG), connecting your mobile device to the Arduino via a USB cable. One of benefits of USB cable means there is no need to connect to the Internet or Bluetooth for the device to function.

    The application is a full-featured IDE that allows the user to write code on a smartphone, download previously written sketches that are stored in Dropbox or Google drive and then start the compilation process.

    The benefits of using the ArduinoDroid app are obvious. Having an IDE at your fingertips allows you to quickly make changes to fields, and the process of attaching an Android device is less complicated and time-consuming than trying to balance a bulky laptop in your arms!

    The obvious disadvantage of the ArduinoDroid application is that writing code on your device may not be a very comfortable experience, especially if you use a smartphone for this purpose. At the same time, this weak point of the application is not so pronounced when on one side of the scale is the convenience of having an ultra-portable method of programming on your board at hand without the need for an Internet connection, and on the other side of the scale is the not very comfortable method of writing code .

    On the other hand, having an ArduinoDroid is an inexpensive way to learn Arduino basics, since an Arduino board clone and USB On The Go cost a few dollars. For those who rarely have access to a computer, the ArduinoDroid app is a great alternative!

    2. Arduino Bluetooth Controller

    The next program on our list is the aptly named Arduino Bluetooth Controller. This application is of great importance in relation to triggers for changes in loaded sketches, and of less importance for Arduino programming. Arduino controller Bluetooth sends data to your board via Bluetooth, giving you the ability to send serial data with the press of a button. You will need bluetooth module for your board, although the module HC-06 widely used and available for as little as $3.

    Worthy of mention is the fact that the program is loaded on English, although the Play Store pictures indicate Italian!

    3. Blynk application for developing projects

    The Blynk application is an excellent development for creating projects. The application's flexibility and simplicity provide an intuitive approach to triggering events on your board. Working with Blynk requires an Internet connection, as the application uses its own own server. You can use either Wi-Fi or mobile data to access the Blynk app, and this feature works great on smartphones.

    One of the strongest points of the application is the variability of connections to the device. With support for almost all development boards, you can connect to the server wirelessly, or using ethernet and even a computer via USB. The service is well documented, and its intuitive application makes it easy to integrate customized control over your project. The Blynk library for the Arduino IDE monitors all communications.

    If you prefer to turn on your coffee machine using your smartphone before getting out of bed early in the morning, this app is truly for you!

    Blynk is not the only service in this category. It is also worth paying attention to such an extremely customized service as Thinger.io and the almost unlimited, albeit extremely difficult OpenHAB. Of the three services, Blynk is the fastest to get up and running, although learning OpenHAB is a great idea in the long run.

    4. Communication from scratch

    The applications described above assume the use of already existing services, which assist you in providing various connection options. What you need to do to exercise complete and total control over every aspect of your applications Android devices? Why don't you solve this issue yourself and from scratch?

    The problem of maintaining control over a package of applications is solved simply by opening USB connection and back-and-forth serial data transfer between applications and the Arduino board. This control option is one of the best to get acquainted with. Android Studio and application creation in general.

    It should be noted that if there are methods and methods for creating applications for devices on the Android platform without code, learning the basics of coding is also worth attention software in Java.

    5. Turn your Arduino into a server

    An alternative way to communicate with your board is to turn it into a tiny server. Key Advantage This transformation of the board into a server is the emergence of the ability to communicate with the boards from any device that can navigate by IP address or send a web request. This will require attachment Ethernet shield to your board to your home network.

    If you don't have an Ethernet shield , then a similar effect can be achieved through a Wi-Fi shield or through a board connected to Wi-Fi, like NodeMCU.

    If node.js code is your jam, it might make sense to take a look at the arduino-android github project. Once again, we repeat that Android applications are developed based on open source, and all you need to do is install the node.js server on the Arduino board of your choice.

    6. Infrared control

    If you are looking for a universal communication tool with your Arduino or you would like to play the role of the legendary secret agent MacGyver, then remove infrared receiver(IT) from your old stereo or VHS player and use it to communicate with your Arduino board!

    Would you like to send text message from your Android smartphone to your Arduino board? This article tells you how to do it!

    What you need

    • Android smartphone supporting USB host mode (i.e. OTG support) - most devices running Android 3.1 and higher support this mode. Test your phone using the USB Host Diagnostics App from the Play Store;
    • Arduino - any version. I'll be using Uno R3;
    • USB cable for Arduino;
    • USB OTG cable- you need it to connect the Arduino USB cable to micro-USB port telephone;
    • Android Studio - you need to install it. It's quite easy to do. Android Studio makes app development easier with its assumptions and code generation. This is one of best IDEs. You can also use this article as a guide to Android installation IDE.

    Basic components of an Android application

    There are three main files in an Android application:

    MainActivity.java This is where the executable Java code is located that controls how the application will function. activity_main.xml Contains the application layout, that is, components: buttons, text display components, etc. AndroidManifest.xml Here you define when the application should run, what permissions it needs, and what hardware it needs to access.

    There are many more files, but they are all connected to each other using these three.

    An activity can be characterized as a screen where the user interacts with the phone. Activities contain widgets such as buttons, text fields, images, etc. that help in conveying information. This tutorial will only use one activity, MainActivity, which will accept user input to send to the Arduino and also display the received text.

    Layout

    We will use the same layout as the USB App and Bluetooth App. It is simple and contains the minimum widgets needed to test the connection between devices.

    As you can see, it contains an EditText widget to receive data from the user, buttons to start the connection, transfer data, end the connection, and clear the TextView . The received data is displayed in a TextView (empty space below the buttons).

    Here is part of the XML code. Because the code for the buttons is similar, it is not shown here. The full code can be downloaded from the link at the end of the article.