Thursday, July 12, 2018

Espressif ESP32 Tutorial - Programming (Arduino IDE)

Introduction


As promised in the previous post, let's have a look at the different ways we can program our ESP32. I will start with the Arduino IDE, because I think that will be the most common option. In this post I will only be looking to get the "hello world" of IoT going, that is blinking a LED.



Once I've selected an IDE, we will do something useful with the ESP32 (stay tuned).

Arduino IDE




Installing support for the ESP32 IC is not yet available through the Boards Manager, so the
instructions on the GitHub page should be used. For the Mac OS, it is pretty straight forward.

1. Install the latest version of the Arduino IDE.
2. Open Terminal and execute the following command (copy->paste and hit enter):

mkdir -p ~/Documents/Arduino/hardware/espressif && \
cd ~/Documents/Arduino/hardware/espressif && \
git clone https://github.com/espressif/arduino-esp32.git esp32 && \
cd esp32 && \
git submodule update --init --recursive && \
cd tools && \
python get.py

3. Restart the Arduino IDE.

Support for the ESP32 for Arduino is under constant development, but once everything is installed,
the sketch writing and upload process is similar to other boards. If you are using the Jaycar board, select ESP32 Dev Module as the board type, and ensure that the correct serial port is selected.



Connect your ESP32 Dev Board to your PC via a USB cable and you should be ready to compile and upload your first program. First check that you can see the port which is connected to the ESP32.

On my MacBook Pro (early 2015) running High Sierra, the port on the Duinotech ESP32 Dev Board was not recognized. If this happens, you may need to install the drivers for the USB-serial converter for this board. It uses a CP2102 IC, and the drivers are found on the Silicon Labs CP2102 website:
https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers.

After you download the correct driver version for your OS. You will need to install it. On the Mac this involves, unzipping the archive, mounting the DMG disk image and then running the Silicon Labs VCP Driver.pkg. You will need to give permission to run this driver in the Security and Privacy preference (this will pop up). You should now be able to see the port in the Arduino IDE (no need to reboot or restart the IDE).

Open up the Blink Sketch (File -> Examples -> 01. Basics -> Blink), compile and download to your ESP32.

You will probably get this error:

'LED_BUILTIN' was not declared in this scope

There are a couple of traps for new players here,

The standard Arduino boards define the LED_BUILTIN macro in the variant file but apparently the ESP32 Dev Board doesn't. For the Arduino, the built in LED is usually connected to pin 13, on the ESP32 it can be anything. The usual suspects are pins 2, 5 or 16. You can fix this error by adding the following line to the top of the sketch, above the setup() function. For example, if your LED is connected to pin 2:

int LED_BUILTIN = 2;

Unfortunately, the Duinotech ESP32 Dev Board doesn't seem to have a LED connected to a user controllable pin. It does have a blue LED which flashes when communicating via the serial port. It is difficult to be 100% sure that this LED is not controllable since I can't find a schematic for this board. Most development boards have a red power LED and a blue in built LED for user control. Anyway, I resorted to connecting an external LED with a current limiting resistor to GPIO 2.



Note that when the EP32 Dev Board is placed on a standard sized bread board, there will only be one spare row of pins, make sure that this is on the side you want to connect to.

Calculation of the current limiting resistor is done using this formula:

Resistor Value = (Vs – VF)/ IF = 160 ohms (I used 150 ohms since that is what I had)

Where:

Vs = Input Supply DC voltage = 3.3V
VF = LED Forward Voltage = 1.7V
IF = LED current = 10 mA



I measured the LED forward voltage drop (VF) using my LCR meter but you could just get it of the data sheet.

I didn't need to do this, but if you are having trouble uploading, try holding the ‘BOOT’ button while pressing and releasing the ‘RST’ button. This should put the board into boot-loader mode to allow uploads.

While you are here you may want to try some of the other ESP32 example sketches provided. For example ChipID (File -> Examples -> ESP32 -> ChipID) will display your chips MAC address in the serial monitor.



All up that wasn't too painful. I think the Arduino IDE would be a good option if you are already familiar with it and as long as the functionality that you need is available. Not all of the libraries are ESP32 compatible yet (e.g. analogWrite is not available yet although there are work arounds).

Wednesday, July 11, 2018

Espressif ESP32 Tutorial - Getting Started

Introduction


I've just started using the ESP32 for my IoT projects and I wanted to record the experience. So here we are! The ESP32 makes for a great low power IoT sensor hub and is a more capable alternative to the Arduino. The caveat is that there is a bit more mucking around to get it to work. There is also some complexity in getting a dual-core microcontroller to play along with a real-time operating system.

I purchased my board from Jaycar in Australia, I'm sure you can get cheaper versions ordering direct from China.

The toolchain for programming the ESP32 in C is a bit of a pain. You’ll need Espressif’s software library (esp-idf), a cross-compiler and build tools that are specific to the chip (xtensa-esp32-*), and a utility to flash the resulting binary file to the device. There are a few different options to program the ESP32, including:

  1. ESP-IDF - the official IoT development framework for ESP32.
  2. Arduino IDE - requires an add on.
  3. Microsoft Visual Studio Code
  4. Eclipse
  5. PlatformIO
  6. EmbedXcode

In the next post I'll try a few of these to see if any offer advantages over the others.

MicroPython




MicroPython is a complete development environment that runs on the ESP32 processor.

The install is done by flashing a firmware image to the board, and then accessing the serial terminal
running at 115200 Baud to enter commands directly into the interpreter. The image can be
downloaded from this page: https://micropython.org/download/#esp32

The esptool.py program will be installed if you have installed the Arduino addon (it’s what does the
uploading under Arduino), otherwise, it can be installed from its GitHub page at: https://github.com/espressif/esptool.

ESP32


The ESP32 is a dual core 32 bit microcontroller featuring WiFi and Bluetooth, and is able to be programmed with the Arduino IDE through an ESP32 add on. Note that not all libraries or functions that work with the ESP8266 or Arduino are yet functional on this new board.

It utilises the Xtensa® dual-core 32-bit LX6 microprocessor running at 240MHz, which is a step up from the ESP8266, while having similar power consumption.

The ESP32 ESP32 can also interface with other systems to provide Wi-Fi and Bluetooth functionality through its SPI / SDIO or I2C / UART interfaces. The ESP32 module comes preloaded with a ROM image of the AT command set, just like the ESP8266 did. If you want to waste 95% of this chip’s potential by using it as a glorified serial-to-WiFi modem, you are good to go!

The operating voltage of ESP32 ranges from 2.3V to 3.6V. When using a single-power supply, the recommended voltage of the power supply is 3.3V, and its recommended output current is 500 mA or more. My board includes a 5V to 3.3V regulator to allow you to power it via USB.



ESP32 has 34 GPIO pins which can be assigned various functions by programming the appropriate registers. There are several kinds of GPIOs: digital-only, analog-enabled, capacitive-touch-enabled, etc. Analog-enabled GPIOs and Capacitive-touch-enabled GPIOs can be configured as digital GPIOs.

Most of the digital GPIOs can be configured as internal pull-up or pull-down, or set to high impedance. When configured as an input, the input value can be read through the register. The input can also be set to edge-trigger or level-trigger to generate CPU interrupts. For low-power operations, the GPIOs can be set to hold their states.

The ESP32 integrates 12-bit SAR ADCs and supports measurements on 18 channels (analog-enabled pins). Some of these pins can be used to build a programmable gain amplifier which is used for the measurement of small analog signals. The ULP-coprocessor (Ultra Low Power) in ESP32 is also designed to measure voltage, while operating in the sleep mode, which enables low-power consumption.  With the appropriate setting, the ADCs and the amplifier can be configured to measure voltage on 18 pins maximum.

The ESP32 has 10 capacitive-sensing GPIOs, which detect variations induced by touching or approaching the GPIOs with a finger or other objects. The low-noise nature of the design and the high sensitivity of the circuit allow relatively small pads to be used.

ESP32 vs ESP8266


The predecessor of the ESP32 was the ESP8266. The feature comparison between the two boards is shown below.




Feature Summary




You can download the ESP32 data sheet but a summary of the ESP32's features are as follows:
  • 240 MHz dual core Tensilica LX6 microcontroller with 600 DMIPS
  • Integrated 520 KB SRAM
  • Integrated 802.11 b/g/n HT40 Wi-Fi transceiver, baseband, stack and LwIP
  • Integrated dual mode Bluetooth (classic and BLE)
  • 16 MB flash, memory-mapped to the CPU code space
  • 2.3V to 3.6V operating voltage
  • -40°C to +125°C operating temperature
  • Onboard PCB antenna / IPEX connector for external antenna


Sensors:
  • Ultra-low noise analog amplifier
  • Hall sensor - detects magnetic fields
  • 10x capacitive touch interfaces
  • 32 kHz crystal oscillator


34 x GPIO:
  • 3 x UARTs, including hardware flow control
  • 3 x SPI
  • 2 x I2S
  • 18 x ADC input channels
  • 2 x DAC
  • 2 x I2C
  • Infrared Remote Control
  • PWM/timer input/output available on every GPIO pin
  • OpenOCD debug interface with 32 kB TRAX buffer
  • SDIO master/slave 50 MHz
  • Supports external SPI flash up to 16 MB
  • SD-card interface support


Security Related:
  • WEP, WPA/WPA2 PSK/Enterprise
  • Hardware accelerated encryption: AES/SHA2/Elliptical Curve Cryptography/RSA-4096


Performance:
  • Supports sniffer, Station, SoftAP and Wi-Fi direct mode
  • Max data rate of 150 Mbps@11n HT40, 72 Mbps@11n HT20, 54 Mbps@11g, and 11 Mbps@11b
  • Maximum transmit power of 19.5 dBm@11b, 16.5 dBm@11g, 15.5 dBm@11n
  • Minimum receiver sensitivity of -97 dBm
  • 135 Mbps UDP sustained throughput
  • 5 μA power consumption in Deep-sleep

Wednesday, April 18, 2018

Custom Arduino Shield in Fritzing

I use Fritzing to develop prototypes and design simple PCB's. Although there is vigorous discussion on the merits of Fritzing compared to other open source free tools, I find it meets my needs.

One thing that wasn't obvious (too me at least), was how to create your own custom Arduino shield without having to work out where to place all the pins so it would mount correctly on an Arduino.

Figure 1. Select Arduino Shield from shape - properties.


This turns out to be fairly straight forward when you know how. Here are the steps:

  1. Open Fritzing (download it if you don't already have a copy - there is a version for all the major OS's).
  2. Select the PCB tab. Tap anywhere on the blank PCB and then change the shape to Arduino Shield (Figure 1) in the properties shape field on the right hand side of the screen. The PCB should then change from a rectangle to the shape shown in Figure 1.
  3. In the parts section, select the Arduino group and drag an Arduino Uno part across to match the position of the PCB. This will give you the pins and their label. Job done!
  4. You can then design your custom shield in the breadboard or schematic tab and connect to the pins on the Arduino to connect to the correct pins on the shield.




Wednesday, April 11, 2018

Arduino Self Levelling Drone (Part 5)

Loading the QuadCopter Firmware




The first step is to download the firmware and save it in your Arduino library directory. This zip file will include 3 Arduino sketches:

  1. YMFC-AL_setup.ino
  2. YMFC-AL_esc_calibrate.ino
  3. YMFC-AL_Flight_controller.ino

Download the setup sketch to your Arduino and open up the IDE serial monitor (Tools -> Serial Monitor) and follow the instructions. If you get any errors then the firmware author has an FAQ. DO NOT connect the battery at this stage.



The complete setup will look something like the following. Make sure that you assign the pitch, roll and yaw functions to different receiver channels. The usual setup is called mode 2 which assigns pitch to the right stick up/down, roll to the right left/right, throttle to the left stick up/down and yaw to the left stick left/right.

Make sure that you check the high, low and centre values for the four digital input channels from the receiver. The low should be close to 1000, the middle near 1500 and the high around 2000. I had to run setup a few times before the correct values were recorded.

===================================================

Your
  Multicopter
    Flight
      Controller

YMFC-AL Setup Program

===================================================
For support and questions: www.brokking.net

Have fun!

===================================================
System check
===================================================
Checking I2C clock speed.
I2C clock speed is correctly set to 400kHz.

===================================================
Transmitter setup
===================================================
Checking for valid receiver signals.. OK

Place all sticks and subtrims in the center position within 10 seconds.
9 8 7 6 5 4 3 2 1  

Center positions stored.
Digital input 08 = 1492
Digital input 09 = 1492
Digital input 10 = 1516
Digital input 11 = 1492


Move the throttle stick to full throttle and back to center
Throttle is connected to digital input 10
Channel inverted = no


Move the roll stick to simulate left wing up and back to center
Roll is connected to digital input 8
Channel inverted = no


Move the pitch stick to simulate nose up and back to center
Pitch is connected to digital input 9
Channel inverted = yes


Move the yaw stick to simulate nose right and back to center
Yaw is connected to digital input 11
Channel inverted = no


Gently move all the sticks simultaneously to their extends
When ready put the sticks back in their center positions
Measuring endpoints....


High, low and center values found during setup
Digital input 08 values:1008 - 1492 - 1984
Digital input 09 values:1004 - 1492 - 1988
Digital input 10 values:1012 - 1516 - 1988
Digital input 11 values:1492 - 1492 - 1500
Move stick 'nose up' and back to center to continue

===================================================
Gyro search
===================================================
Searching for MPU-6050 on address 0x68/104
MPU-6050 found on address 0x68

===================================================
Gyro register settings
===================================================
Register 0x6B is set to:0
Register 0x1B is set to:1000

===================================================
Gyro calibration
===================================================
Don't move the quadcopter!! Calibration starts in 3 seconds
Calibrating the gyro, this will take +/- 8 seconds
Please wait....................
Axis 1 offset=-99.78
Axis 2 offset=-66.14
Axis 3 offset=-60.42

===================================================
Gyro axes configuration
===================================================
Lift the left side of the quadcopter to a 45 degree angle within 10 seconds
OK!
Angle detection = 1
Axis inverted = no
Put the quadcopter back in its original position
Move stick 'nose up' and back to center to continue


Lift the nose of the quadcopter to a 45 degree angle within 10 seconds
OK!
Angle detection = 2
Axis inverted = yes
Put the quadcopter back in its original position
Move stick 'nose up' and back to center to continue


Rotate the nose of the quadcopter 45 degree to the right within 10 seconds
OK!
Angle detection = 3
Axis inverted = yes
Put the quadcopter back in its original position
Move stick 'nose up' and back to center to continue

===================================================
LED test
===================================================
The LED should now be lit
Move stick 'nose up' and back to center to continue

===================================================
Final setup check
===================================================
Receiver channels ok
Gyro axes ok

===================================================
Storing EEPROM information
===================================================
Writing EEPROM
Done!
Verify EEPROM data
Verification done
Setup is finished.
You can now calibrate the esc's and upload the YMFC-AL code.

After the setup is completed all the settings are stored in the EEPROM of the Arduino.



Checks and Calibration


The following is reproduced from the web site of the firmwares author.

To make sure that everything is working correct it's necessary to run some basic checks. Remove the props, disconnect the flight battery and upload the ESC calibration program to the Arduino. Open the serial monitor at 57600baud.

Receiver input check


Send the letter 'r' to start the receiver monitor. Now move the sticks and see if the values on the screen correspond with the movements of the sticks.


All the channels should read 1000us till 2000us with a center position of 1500 (+/-8).


Gyro / accelerometer angle check


After the receiver check is completed send the letter 'a' to start the angle check.


Don't move the quadcopter because the gyro needs to calibrate itself. After the calibration the roll and pitch angles are shown. The yaw value is the output of the gyro and will go back to zero if the yaw rotation stops.


Check if the angles correspond with the movement of the quadcopter:

Nose up is positive pitch and nose down is negative pitch.
Left wing up is positive roll and left wing down is negative roll.
Nose right is positive yaw and nose left is negative yaw.

Calibrate the ESC's


Electronic speed controllers or ESC's for short are controlled with a 1000us till 2000us pulse. 1000us means off and 2000us means full throttle. To make sure that all the ESC's react the same way it's important to calibrate the 1000us and 2000us point. Without calibration the motors will perform different and the quadcopter doesnt fly well or might even crash.


Remove the props and upload the ESC calibration program to the Arduino. Disconnect the USB cable and follow the instructions in the manual to calibrate the ESC's.




In most cases ESC Calibration works as follows:
  1. Throttle to minimum and turn on the transmitter.
  2. Place the throttle stick in the upper position (full throttle).
  3. Connect the flight battery.
  4. Wait for your ESCs to emit a musical tone, the number of beeps indicates your battery’s cell count (i.e. 3 for 3S, 4 for 4S) and then an additional two beeps to indicate that the maximum throttle has been captured.
  5. Pull the transmitter’s throttle stick down to its minimum position.
  6. The ESCs should then emit a long tone indicating that the minimum throttle has been captured and that calibration is complete.
  7. f the long tone indicating successful calibration was heard, the ESCs are now “live” and if you raise the throttle a bit they should spin. Test that the motors spin by raising the throttle a bit and then lowering it again.
  8. Disconnect the flight battery

If possible check the manual of your specific ESC for the correct calibration procedure. The ones I purchased didn't come with any instructions but the above procedure worked.

It was at this stage that I smoked a couple of my ESC's and motors. I don't know what caused this but replacing them got everything working. I suspect that the cheap ESC's and motors may have some quality control issues. If you do buy the cheap ones that I purchased then I suggest getting an extra set. In retrospect I would probably buy better quality ESC's and motors to avoid the aggravation of having to re-solder everything. If smoke starts coming out of your ESC's or motors (or anything for that matter) disconnect the battery immediately! If smoke is just coming out of the motors, replace both the ESC and the motor, I burnt out an extra motor because I didn't do this.

Burnt out ESC - you can see where it melted through the heat shrink.

It is also possible to calibrate the ESC's individually but you need to do this before you cut off the servo plugs on the ESC control cables and solder them to the Arduino. For completeness the procedure for individual calibration is:

  1. Plug one of your ESC three-wire cables into the throttle channel of the RC receiver. (This is usually channel 3.)
  2. Turn on the transmitter and set throttle stick to maximum (full up).
  3. Connect the LiPo battery
  4. You will hear a musical tone then two beeps.
  5. After the two beeps, lower the throttle stick to full down.
  6. You will then hear a number of beeps (one for each battery cell you’re using) and finally a single long beep indicating the end points have been set and the ESC is calibrated.
  7. Disconnect battery. Repeat these steps for all ESCs.
  8. If it appears that the ESC’s did not calibrate then the throttle channel on the transmitter might need to be reversed.
  9. If you are still having trouble after trying these methods (for example, ESCs still beep continuously) try lowering your throttle trim 50%.

You shouldn't need to do individual calibration but it is available if the all at once method doesn't work.

Attach the Propellers


Attaching a collet style prop adapter is not obvious the first time you attempt it. This is probably one of the most common prop adapter you see people using. It has a collet that grabs the smooth motor shaft when propeller nut and washer is tightened down.

The first step is to slip the collet onto the motor shaft. This should be a tight fit, otherwise it is difficult to screw down the nut without everything spinning. Some people suggest putting Loctite on the shaft, this is probably not a bad idea but wait until you have performed all the tests below and are doing the final tighten up before flying. I tried it without Loctite but on the first flight the propeller spun off, luckily it was still on the ground at the time. So I went back and put Loctite on the motor shaft and the propeller nut. Make sure that none of it drips down into the motor. I used the blue Loctite so that there was some chance I could get it off when I needed to replace a propeller. The red Loctite requires heat to remove it, which is a problem if you have plastic propellers.



Next slip on the spacer which grabs the bottom of the propeller. Make sure that you put it the right way around.



Finally put on the propeller and screw down the prop nut to keep everything in place. Note the props are designed to be clock wise or counter clockwise. They normally come in pairs. Make sure you mount the correct prop on each motor (refer to Figure 1 below). You want the leading edge of the props to face in the direction of the rotation shown below. The leading edge is the highest edge on the prop.



You can use a drill bit or similar to tighten the prop nuts, these need to be secure but be careful not to strip the aluminium thread. Finger tight should work.



Check Rotation Direction and Balancing the Propellers


The next step is to check that the props are spinning in the right directions and that they are balanced. Adjacent props spin in the opposite direction to keep the drone from yawing. This provides a similar function to the tail rotor on a helicopter. The props need to be balanced to minimise vibration which will prevent the gyro and accelerometer from self levelling.



We will again use the ESC calibration software for these checks. For convenience we have reproduced the balancing instructions from the Brokking.net web site. You shouldn't need to reload the ESC calibration software.

Make sure that you hang onto the drone when you test with the propellors mounted. Learn from my mistake - even one propellor has enough lift to flip the drone off the table and onto the floor!

TAKE CARE WHEN DOING THE FOLLOWING TESTS - THE DRONE CAN BE DANGEROUS WITH THE PROPELLERS INSTALLED!!! MAKE SURE YOU TAKE APPROPRIATE PRECAUTIONS, KEEP FINGERS CLEAR OF PROPS AND WEAR SAFETY GLASSES.

Mount the props on the motors and check if the counter clock wise and clock wise props are in the right position. Upload the ESC calibration program and open the Arduino serial monitor at 57600baud. Send '1' via the serial monitor and wait for the response "Test motor 1 (right front CCW.)".

The numbers that are printed on the screen represents the amount of vibration measured by the accelerometer. This is not a standardized value and should only be used to minimize the amount of vibration of your YMFC-AL quadcopter.

Hold the quadcopter firmly down, place the throttle in the lowest position and connect the flight battery. Now slowly increase the throttle until motor 1 starts to spin. Check the direction of rotation and that the prop produces upward thrust. If the motor rotates in the wrong direction you need to switch two of the three motor wires. Put the throttle in the lowest position to stop the motor.

Now hold the motor frame firmly in your hand and increase the throttle to half throttle. Check the numbers on the screen and also memorize the vibrations that you feel with you hand that is holding the motor frame.

Stop the motor and put a small piece of tape on one of the blades and run the test again. Check if the vibrations reduce. If nor try a piece of tape on the other blade. Keep doing this until the motor and prop run as smooth as possible. This can sometimes be a daunting task but the reward is a very stable flying quadcopter. So take your time and get it perfect!

When done with motor 1, send a '2' via the Arduino IDE and start the process again for motor / prop 2. And after that, send a '3' for motor number 3 and a '4' for motor 4. By sending a '5' all the motors will run together as a final test.




Flight Controller Software


We are finally ready to load the flight controller software. Disconnect the battery and upload the YMFC-AL_Flight_controller sketch to the Arduino. When done, disconnect the USB cable and find a spot to test your drone.

Always turn on your transmitter (with throttle in the minimum position) before connecting the flight battery. This is a good habit to get into.

Hold the quadcopter firmly in your hand and start/stop the motors with the following sequence:

Start = throttle down and yaw left
Stop = throttle down and yaw right

If you have flown any DJI drones this may be the opposite to what you are used to. We want to test the start/stop function before we do anything else.

Start the engines again and increase the throttle up to the point when it almost starts to become weightless. The quadcopter should now try to level itself. If you move the quad it should start to counteract the movement until it is level again. This is an important step, you need to make sure that the drone is balanced and able to self level before you try and fly it in the wild. If you don't it may be very difficult to fly - I learned this the hard way...

When the roll or pitch stick of the transmitter is moved the quadcopter should move in the same direction. If this is not the case redo the setup procedure and double check the settings.



Next up we will cover the procedure for the first flight. You need to become familiar with the rules for flying drones in your local area, particularly if you are going to be stupid enough to post a video on YouTube of you doing the wrong thing. CASA is watching!



Arduino Self Levelling Drone (Part 4)

Connecting Power to the Arduino




There are a number of different ways that you can power your Arduino. To select which one is most appropriate for our Drone we need to consider each option.



1 – USB Port: this is good for prototyping and where you have little or no external current requirements or voltage greater than 5 V. You can supply 5 V via the USB port and this enables the dual function of powering and programming the board. The current limit imposed by the PTC self-resettable fuse is 500 mA, but usually the fuse will tolerate up to almost twice that value before blowing. The LiPo battery will deliver over 12 V when fully charged (nominal is 11.6 V), so this option is not appropriate for the drone. Note also that if you are powering the Arduino via USB from a computer, there is a current limitation of on that port of 250 mA or 500 mA, (depending on the USB port type).

2 – The 2.1 mm DC JACK Port: You can connect between 6 and 20 V to this jack. The onboard Arduino regulator will provide 5 V for the board and external devices if required. Practically, you want to supply between 7 and 12 V. The lower limit allows for voltage loss via the protection diode and regulator, while the upper limit will prevent the regulator from running too hot. The jack needs to be wired centre positive. If you provide power via the DC jack then you can access this (less the voltage drop from the protection diode) from the Arduino Vin pin. This can be useful if you need voltages other than 5 V.



3 – Vin Arduino Pin: this pin can be used as either an input or output.

3a: input for external powering, note that it is NOT protected against reverse polarity. The Vin pin connects to the regulator’s input and after the DC JACK protection diode. Don't ever connect voltage to the DC jack and the Vin pin.

3b: output from which to draw the voltage applied to the JACK socket, less the protection diode’s voltage drop. This can be used to power small loads, requiring a voltage higher than 5 V and equal to the voltage applied to the DC JACK socket (less the diode drop).

In both cases the voltage negative is the Arduino GND pin.

The 3.3V and 5V Pins

The 5V pin is directly connected to the regulator’s output, and you can use this to power external loads. If no voltage is applied to the USB Port or to the DC JACK, then the 5 V pin could be used to power the Arduino but it is not recommended. In general, regulators do not like voltages being applied to their output, but this happens even when powering the Arduino from the USB port, therefore we assume that the board can cope with this. Nevertheless it is still dangerous since there is no protection (both the diode and the PTC fuse are up stream of this pin).

There is another regulator connected to the output of the 5V regulator which delivers 3.3V for use in external loads. It can not be used as a voltage input for the Arduino.

Conclusion

Given all the above, for the drone we will connect the LiPo battery via the DC jack. This gives us diode reverse polarity protection and can handle the 11.6V or so that we get from the battery (depending on the charge state). Using a 2.1 mm plug also makes it easy to connect and disconnect the Arduino power.



Use the wires that come from the Power Distribution Board and connect to the battery (red and green in the photo above). The plug is centre positive (i.e. connect the middle pin to the red/positive cable). Make sure you put heat-shrink on the cables before soldering.



Once you have wired the plug, check continuity using your multimeter. The LiPo battery dean plug makes an easy test point.



Finally you can insert the 2.1mm plug into the Arduino DC JACK. While you are there, also insert the Right Angle Headers (green in the photo below).



Wiring up the Arduino IO


MPU-6050




We will start with wiring up the MPU-6050 I2C connections. It is easier if you pre-tin the 90 degree Arduino headers and the wire. Hopefully you took note of which colour wire connects to the SDA and SCL connections on the MPU-6050 (see Part 2 in this tutorial). In our case:

BLUE    = SDA
WHITE = SCL

Leave enough slack in the wires so that you can pull out the 90 degree headers (to remove the Arduino) if required. Make sure you put the heat shrink on the cables BEFORE you solder the connection. You need a steady hand.



We will wire up the 5V and GND for the MPU-6050 shortly, we need to do a simple 5V wiring harness to include the receiver as well since we only have one 5V pin on the Arduino.

Indicating LED




Next up is the indicator LED which is wired to D12 on the Arduino. You can calculate the current limiting resistor for your LED using the LED Resistor Calculating site. You will need:

1. Source Voltage: 5V
2. LED Forward Voltage: 1.68V(I measured mine using an LCR meter but you can get it from the data sheet).
3. LED Forward Current: 20mA (nominal/typical from Data Sheet).



This gives a result of 180 ohms for my LED. If in doubt, just use a 330 ohm resistor. Note when wiring LED's that they are polarised.





Wire the positive of the LED to the current limiting resistor, then cover with heat shrink. The resistor end then gets soldered to D12 and the -ve end of the LED to GND. Refer to the photograph below.



Receiver Connections



Now we will do the receiver connections and the 5V bus. Use 5 servos cable for this, as we need the end connector to plug into the receiver. Use one servo cable as the power connection and the other 4 to connect the signal (white wire on my cable). In other words, remove the white wire from one servo cable and remove the red and black wires from the remainder.



The receiver should indicate which way around to plug the servo cables. Plug the power connection to BAT (double check that you have it the right way around) and then plug in the signal wires to channels 1 through 4. You can remove the plugs from the end which goes to the Arduino.



Cut the signal wires to length, strip and pre-tin as before. Have the heat shrink in place before soldering.



The connections are:

Arduino D8    to    Rx CH 1
Arduino D9    to    Rx CH 2
Arduino D10  to    Rx CH 3
Arduino D11  to    Rx CH 4



5V Wiring Harness



Construct a wiring harness to connect the 5V pin on the Arduino to the power on the receiver and MPU-6050.



Similarly, connect a GND wiring harness to connect one of the Arduino GND pins to the receiver, MPU-6050 and ESC BEC GND (brown wire - do this if they aren't already connected via the ESC to the GND on the Power Distribution Board). I connected the two back arm ESC GND's to this harness and the two front ones to the GND used for the voltage divider resistors. Refer to the photographs.



Voltage Divider




Note when wiring the voltage divider that the order of the resistors is important. The 1.5k resistor needs to connect to Vin and the 1k to GND.



The output voltage gets connected to A0 on the Arduino. To make things easier to keep track of, I marked the 1.5k resistor with liquid paper.



Wire the two front ESC GND (brown) wires to the GND side of the 1k Resistor, then cover the resistors in heat shrink.



Finally solder the resistors to Vin and GND and the wire to A0.



ESC Control Lines



The last connections to the Arduino are the four ESC control (orange) wires. The connections are as follows:

Arduino D4  to  ESC 1 (right front - CCW rotation)
Arduino D5  to  ESC 2 (right rear - CW rotation)
Arduino D6  to  ESC 3 (left rear - CCW rotation)
Arduino D7  to  ESC 4 (left front - CW rotation)

Pre-tin the Arduino pins and wires. Put the heats shrink in place before soldering and then make the connections shown above. You can cut off the ESC red 5V BEC wires if you want. These are not used. I'm right handed but I find it easier to hold the wire in place and solder with the left hand in this instance.


That concludes Part 4. In Part 5 we will download the firmware, and calibrate the sensors, ESC's and Receiver. You will need a charged LiPo so get it on the charger.