Ultrasonic Measurement with Microchip’s PIC and AVR Microcontrollers
Gregor Sunderdiek, Microchip Technology Inc
Today ultrasonic measurement is used in a wide range of applications. The main advantage of ultrasonic is that it is contactless and it is used in various levels of measuring applications as nearly all materials can be detected. Some examples are the measurement of liquid or solid products in silos or other containers to prevent overfill or to check the current fill level. Another application area is using ultrasonic measurement to count moving objects. This can be used, for example, to count bottles in a soda filling machine. In automotive, the ultrasonic measurement can be used for parking. The distance detector can measure the distance from the car to a barrier (other car, wall, etc.).
These are just a few examples of where ultrasonic measurement is currently being used though there are many more applications where this technology can be implemented.
The main principle in play here is that the distance or range from the ultrasonic sensor to another object is what is measured. In ultrasonic measurement, the time of flight between when the signal is transmitted to when it is detected by the receiver after bouncing off of a given object is measured. This is the time it takes the original wave and the reflected wave to travel distance r (see figure 1). From this time, distance r can be calculated, as it is known how fast the ultrasonic signal travels through the air (the speed of sound).Though this calculation seems fairly straightforward, the travel time of the ultrasonic signal is also dependent on many other factors. One substantial element that impacts the speed of sound is the temperature of the air through which it is traveling. Other factors include humidity, air pressure, air currents (only strong winds) and the type of gas. Most ultrasonic measurement sensors are designed for use in human-breathable air (approximately 21% oxygen, 78% nitrogen and approximately 1% other trace gases). The sensors which work in this type of air are not guaranteed to work when used in other gases as the speed of sound will change depending on what it is traveling through. The most commonly used frequencies are in the range of 40 kHz, which is above the audible frequency of 20 Hz to 20 kHz.
Ultrasonic Measurement with Microchip’s 8-bit MCUs
Ultrasonic measurement can be done with many different Microchip microcontrollers (MCUs). The PIC16F1769, an 8-bit PIC microcontroller, and the ATtiny817, an 8-bit AVR microcontroller are both well-suited to applications leveraging ultrasonic measurement.
One of the key advantages of developing with an 8-bit MCU from Microchip is their integration of Core Independent Peripherals (CIPs). In ultrasonic measurement applications, the CIPs can be used to efficiently measure the time of flight without involving the Central Processing Unit (CPU). This not only lowers overall power consumption of the system but it also frees up the CPU to execute other tasks.
Another advantage of leveraging CIPs is that the command from one peripheral to another is done in less than a clock cycle. This function can be done in both PIC and AVR microcontrollers. Doing this without CIPs would require involvement from the CPU. It would need to get an interrupt, process the interrupt and then could give a command to a peripheral. This would take several clock cycles - slowing down the system and consuming substantially more power.
Another important benefit to using a microcontroller with CIPs is that less software is needed because more tasks are executed in hardware. Less software development also means less software validation, leading to less risk in the development process. All of these elements equate to a faster time to market.
A solution built with either a PIC or AVR microcontroller will use CIPs to measure the time of flight as well as to generate, transmit and receive the ultrasonic signal. Once the CIPs are initiated, there will be no involvement from the CPU to execute these tasks. No interrupts, CPU resources, SRAM or long program flow are needed to measure the time of flight. The CPU will only be engaged for the calculation of distance r as it will need to multiply the time of flight with the speed of sound. A detailed technical description of this process can be found in the dedicated Application Notes (see additional resources section).
Below is a comparison of two microcontroller options for consideration when looking to do ultrasonic measurement. The ATtiny817 is one of the new tinyAVR microcontrollers introduced in late 2016 while the PIC16F176x/7x MCUs were introduced earlier that year. Both are new families and contain Core Independent Peripherals that enable measurement of the time of flight without CPU usage.
Microchip has two application notes which describe the measurement of distance with ultrasonic transceivers. One uses an 8-bit PIC microcontroller, the PIC16F1769 and the other uses an 8-bit AVR microcontroller, the ATtiny817. Both applications use CIPs to efficiently measure the ultrasonic distance with the least possible code size and involvement of the CPU. Below are the comparisons of the minimum Flash memory, Static Random-Access Memory (SRAM) and I/O requirements (see table 2).
As you can see in Table 1, both MCUs have different sets of peripherals. Table 2 shows the Flash and SRAM requirements of each MCU for their application. This code already includes overhead to show the results on an LCD or OLED display. The measurement of the time of flight is done with the CIPs and without involvement of the CPU and without the need for SRAM. The instruction for the initialization of the CIPs needs only a very low number of bytes in Flash. It is still possible to optimize the code and use a lower Flash density device from the respective PIC or AVR families. For both solutions there is enough space to extend the firmware and customize it to different communication channels to include the ultrasonic sensor in an industrial network or with different serial communications.
Advantages of both solutions
Both solutions can efficiently measure distance with ultrasonic sound. The PIC16F1769 has many integrated analog features, including two operational amplifiers. These are needed for receiving the attenuated ultrasonic signal. The operational amplifier amplifies the receiving ultrasonic signal so that the analog comparator can detect it. These integrated analog features within the PIC16F1769 microcontroller make a solution with minimum external components easily achievable.
The ATtiny817 can be used with two external operational amplifiers for receiving the ultrasonic signal. These boards can also be customized with different external components to address different ranges of needs and capabilities.
With the industry’s largest portfolio of 8-bit MCUs, Microchip is well-positioned to provide a solution that meets the needs of nearly any ultrasonic distance measurement application. Core Independent Peripherals eliminate the need for involvement from the CPU for the measurement of the time of flight. This is done very efficiently and with very little power consumption.
Microchip also has an easy-to-use ecosystem to speed up development cycles. Both series of PIC and AVR MCUs are supported by Microchip’s Integrated Development Environments (IDEs), (MPLAB X for PIC devices and Atmel Studio 7 for AVR devices), configuration/initialization tools (MPLAB Code Configurator (MCC) for PIC MCUs and Atmel Start for AVR MCUs) and tools (development, evaluation boards and programmers/debuggers). Furthermore, the application notes for the PIC16F1796 and the ATtiny817 provide all the details on how to implement ultrasonic range measurement or proximity measurements into an application.
LATEST issue 4/2023