Using Arduino Nano
There are times when you want to drill a hole that needs to be at a precise spot or angle. With that in mind, these authors built a position indicator to help users position their handheld power drill so that the drill hole is oriented as required. The prototype for this battery-operated portable device was crafted using an Arduino Nano development board.
Our position indicator project is a battery-operated portable device aimed at helping users position their handheld power drill so that the drill hole is oriented in a precise spot or at a specific angle. This is useful, for example, when you need to drill a hole that’s perfectly orthogonal to the wall or to a piece wood or other material. The entire system’s block diagram is shown in Figure 1.
Although our prototype makes use of an Arduino Nano development board for providing the orientation of the drill from the sensor to the user, the device’s entire software is written in C language—independent of the Arduino environment. The software is developed using the standard AVR libraries, the GNU avr-gcc compiler and the AVRDUDE utility as a software interface for the programmer. We personally found this project to be a gratifying and exciting experience because it involves register level programming with interrupt handlers. All of this broadens our understanding of the hardware we used and its limitations.
The device’s hardware consists of an Arduino Nano development board, which has a Microchip Technology (formerly Atmel) ATmega328P  microcontroller (MCU) operating at 16MHz with the high logic level of 5V. The board also has a GY-521 inertial sensor module, which is a SparkFun MPU-6050 breakout board used to measure six degrees of freedom (6-DOF) with its MEMS- based accelerometer and gyroscope  (TDK Invensense). Other components include two indicator LEDs, resistors, a capacitor, a momentary switch (toggle switch), jumper links, male and female header pins and two CR2032 coin cell batteries with a battery holder, and a perfboard.
The perfboard has a customized rectangular dimension such that it’s held firm within the hollow support handle provided with the drill. Apart from all those components, we used a solder iron, solder and solder flux to construct the prototype. A PC is required for compiling the program with avr-gcc and for using USBasp hardware (from Thomas Fischl) as our ISP (in-circuit programmer). The schematic diagram of the circuit is shown in Figure 2. A list of the components used in the project is provided in Table 1.
Male headers are soldered to the MCU development board and are inserted into the female headers soldered onto the PCB. The power, SDA and SCL pins from the MCU are connected to the GY-520 sensor module through the header connectors. The measured data from the sensor module is communicated to the MCU through the I2C bus. The momentary switch is connected to the MCU and acts as a user interface. A capacitor is used in parallel to the switch for debouncing. Both the LEDs are also connected to the MCU and act as indicators for the user. The LEDs and the momentary switch form the human machine interface (HMI). The coin cell batteries act as the main power source for the whole circuitry—making it a portable device. A jumper is used as a switch to control the power flow from the batteries. The final prototype we developed is shown in Figure 3.
OPERATING THE PROTOTYPE
The prototype is operated by initially attaching the device firmly to the handheld power drill (by inserting it into the hollow support handle) as shown in Figure 4. Once attached, you position the drill with respect to the wall/surface in the desired orientation for the drill hole (either using the hole present in the fixtures as a guide or using a protractor). While you continuously hold down the momentary switch for more than 1.8 seconds but less than 4.2 seconds—or about two blinks of the red LED (D1)—the device will store the current orientation. This process is what we call the “snapshot.” Now the handheld power drill can be moved freely toward the marked place that needs drilling (we call this the “marker”). You orient the hand drill until both the LEDs turn “OFF.” The less bright the LEDs are, the closer the hand drill is oriented toward the actual stored orientation. When the LEDs turn OFF completely, the drill is aligned within the limits of the snapshot position.
The sensitivity of the device can be altered by continuously holding down the momentary switch for more than 8ms but less than 1.8 seconds—or about one blink of the red LED (D1). This works by changing the tolerance limits set for the LEDs to turn OFF. There are four tolerance levels. During each sensitivity change, the tolerance levels are selected in a cyclic manner. After selection, the current sensitivity setting is indicated by the combination of LEDs as shown in Table 2. The device can be reset by continuously holding down the momentary switch for more than 4.2 seconds or about four blinks of the red LED (D1). A “blink” is an entire gradual “ON-OFF” cycle of the red LED (D1).
|Red LED (D1)||Dim||OFF||ON||ON|
|Green LED (D2)||Dim||ON||OFF||ON|
|Sensitivity level||0 – very low||1 – low||2 – intermediate||3 – high|
After a snapshot, orienting the drill for the red LED (D1) to turn OFF is done by pivoting the drill on the plane containing the line of gravity to change the inclination angle with the drill bit on the marker, using the change in brightness of the red LED (D1) as a guide. And orienting the drill for the green LED (D2) to turn OFF is done by pivoting the drill on the plane containing lines perpendicular to the line of gravity to change the azimuth angle with the drill bit on the marker, using the change in brightness of green LED as a guide. The inclination and azimuth angles are represented in the Figure 5.
The firmware we developed is responsible for collecting the sensor data and user inputs. That data and input provides the orientation status to be indicated through the LEDs. The prototype uses the MCU’s internal timer0, timer1 and TWI (two-wire serial interface) for complete operation. Additionally, USART0 was used in asynchronous mode as UART for debugging and communication purposes with the PC through the onboard USB/TTL converter. The flowchart of the firmware developed is shown in Figure 6.
Upon reset, the TWI, UART and GPIO peripherals are initiated, followed by variable initiation. Finally, the timers are initiated, followed by enabling global interrupts. Then the CPU enters a “super loop” and is immediately put to IDLE sleep mode. Upon receiving an interrupt, the CPU wakes, checks for the flags set, takes appropriate actions, then goes to sleep again. With the timer0 and its compare unit A operating at the source clock frequency of 16MHz with the prescaler of 1024, the whole number period of 16ms at the frequency 62.5Hz is achieved by writing the value 124 to the compare register. This means a trigger is generated every 8ms in order to make a 16ms period waveform possible, as shown in Figure 7. This determinism in trigger period is required for precisely calculating the angle traversed by the device using the gyroscope.
The data from the sensor module is collected every 24ms owing to the maximum delay of 19ms required for the sensor module’s digital low pass filter to process the signal at its highest settings. While initiating the TWI driver, an I2C bus freeing algorithm ensures that all the other I2C devices waiting to transmit or receive data are freed up to ensure proper operation. This avoids the prototype from malfunctioning due to an unexpected MCU reset, thereby leaving other devices on the bus waiting for the clock. This is done in accordance with the solution #1 published in the application note AN-686 by Analog Devices . Figure 8 and Figure 9 show the waveforms in action.
This driver contains APIs fine-tuned for the MPU-6050 and uses a repeated start mechanism instead of stop-start signals in places that are apt to use this procedure. This is shown in Figure 10. A link to the MPU-6050’s register map and descriptions at .
Editor’s note: Although the terms “master” and “slave” have long be used in the electronics industry, those terms are discouraged these days for obvious and valid social reasons. The industry as a whole has not yet come to any agreement on replacement terms. For this article (Figure 7 and Figure 8 captions) we’re using the term “control” to replace “master,” and use the term “remote” to replace “slave.”
The program for the MCU is written in C. Microsoft’s Visual Studio Code is the text editor we used for keying in the code on Ubuntu 20.10. To program the MCU, it must be attached to the programmer and the programmer must be attached to the PC as shown in Figure 11. The pin-to-pin correctness of the harness connecting both the MCU and the programmer should also be verified. The entire GNU AVR toolchain can be installed in ubuntu using the command:
sudo apt-get install gcc-avr binutils-avr gdb-avr avr-libc avrdude
Upon installing the tool chain, the code is entered into the text editor and can be compiled using the avr-gcc tool. Once the source code is entered within the file named
<filename>, it can be compiled using the command:
avr-gcc <filename> -O3 -mmcu=atmega328p
Now the compiled ELF file resulting from the compilation process (named a.out) is converted to a hex file (named a.hex) as needed by the AVRDUDE utility. This is done with the tool avr-objdump using the command:
avr-objcopy -j .text -j .data -O ihex a.out a.hex
The hex file is then uploaded to the flash of the ATmega328P MCU connected to the PC through the programmer USBasp with the AVRDUDE tool using the following command:
avrdude -p m328p -c usbasp -U flash:w:a.hex:i -P usb
Once the MCU is programmed, connect the MCU to the prototype board through the header pins. The source code can be found at .
The snapshot is the primary feature of the device that enables users to operate the handheld power drill with ease. It lets them operate the drill without worrying too much about the orientation of the drill hole because it is indicated by the LEDs in an easily visible manner. That means you don’t have to place complicated reference objects on your surface while operating the drill, which might not always be possible and can be difficult to keep in position. Now users can keep their focus on the power drill with less distraction, which avoids safety hazards. Instead of orienting and drilling simultaneously, this device enables the user to orient first, drill next. This makes the drilling a sequential task, enabling the user to focus on one thing at a time.
 ATmega328P datasheet, Link
 MPU6050 datasheet, Link
 Jim Greene, Application Note AN-686, Analog Devices, Link
 MPU6050 register register map and descriptions, Link
Analog Devices | www.analog.com
Hantek | www.hantek.com
Microchip Technology | www.microchip.com
SparkFun Electronics | www.sparkfun.com
TDK Invensense | www.invensense.tdk.com
Thomas Fischl | www.fischl.de
PUBLISHED IN CIRCUIT CELLAR MAGAZINE • JULY 2021 #372 – Get a PDF of the issue