The world of ultraviolet sensors has evolved a lot in recent years and the AS7331 has become an essential reference. For those who want to measure ultraviolet (UV) radiation with high precision and reliability. If you're an Arduino enthusiast, an engineer, a scientist, or simply curious about advances in environmental sensing, you've surely encountered the need to accurately record UV exposure. In this article I'm going to tell you everything you need to know about the AS7331 ultraviolet sensor, how to get the most out of it, and how to connect it to Arduino to monitor UV radiation in real time..
Despite its small size, the AS7331 packs a lot of technology.. Thanks to its high sensitivity and three-independent UV channel architecture, it's perfect for all kinds of applications: from educational projects and home weather stations to industrial security systems and scientific studies where precision is key. And if you've ever struggled with difficult-to-integrate sensors, you'll be happy to know that thanks to the SparkFun Qwiic board and its Arduino-specific library, working with the AS7331 will be easier than you think.
What is the AS7331 UV sensor and why is it so special?
The AS7331 is a UV sensor developed by ams OSRAM which allows measuring ultraviolet radiation in three key bands: UVA (320-400 nm), UVB (280-320 nm) and UVC (200-280 nm)Each channel has its own photodiode and interference filter, providing extreme sensitivity and outstanding accuracy, even at low radiation levels and under varying lighting conditions.
The sensor comes in a breakout board format, like the one from SparkFun, facilitating solderless connection thanks to the Qwiic system (standard 3,3V, GND, SDA and SCL connectors) or, if you prefer, using traditional 2,54 mm headers. Its compact size (less than 3 cm long) and low energy consumption making it an ideal choice for both stand-alone battery-powered systems and fixed projects.
Technical characteristics and functionalities of the AS7331
- Three independent UVA, UVB and UVC channels, each with its own dedicated photodiode and filter.
- Four modes of operation to suit different needs: Single measurement (One-shot or CMD), Continuous measurement (CONT), Synchronized start (SYNS) and Synchronized start/end (SYND).
- Very high dynamic range (up to 3,43E+10 thanks to the combination of resolution and adjustable gain), ideal for measurements in extreme or changing environments.
- Low energy consumption: 1,42 mA active, 970 µA idle, and only 1 µA deep off.
- Communication via I²C with the possibility of assigning four addresses (0x74 to 0x77) using solder bridges, facilitating the use of several sensors on the same bus.
- Simple connectivity thanks to the dual Qwiic connectors and additional pins for interrupts and sync.
- Supply voltage of 2,7V to 3,6V, compatible with most Arduino boards and embedded systems.
What is the AS7331 UV sensor used for?
Thanks to its features, the AS7331 is useful for much more than just measuring the typical UV index.
- Environmental monitoring: UV radiation control in meteorological stations and laboratories.
- Personal UV exposure monitoring: integration into wearables or portable devices to avoid risks of overexposure to the sun.
- industrial applications: control of processes that depend on UV exposure, such as resin curing, disinfection or quality control in manufacturing.
- Scientific investigation: study of the effects of UV radiation on different materials, living organisms or crops.
- Educational and maker projects: Use in classrooms, workshops or as a teaching aid to learn about optical sensors and ultraviolet radiation.
Operating modes: choose the one that suits your project
One of the strong points of the AS7331 is its versatility in operation. It has four modes:
- Single Shot (CMD): The sensor activates for only one measurement and returns to low-power mode. This is the ideal option for projects where battery life is a priority.
- Continuous measurement (CONT): The sensor continuously takes measurements and sends data. This is very useful if you need constant monitoring, although the power consumption will be somewhat higher.
- Synchronized Start (SYNS): Allows measurement to be triggered via an external signal. It's ideal for synchronizing the sensor with other devices or events.
- Synchronized start/end (SYND): Both the start and end of the measurement are controlled by signals, increasing control over the data acquisition process, perfect for experiments where timing is essential.
This flexibility makes the AS7331 not only powerful, but also adaptable to very diverse needs., from the classic Arduino hobby to demanding industrial applications.
Connecting the AS7331 to Arduino: hardware and connections
The most popular version of this sensor is the SparkFun Qwiic Spectral UV Sensor AS7331. It is designed to make life easier for both beginners and experts:
- Double Qwiic connector: You save soldering and avoid wiring errors by connecting directly to Qwiic-compatible Arduino boards.
- 2,54mm Pass-Thru Head: If you prefer standard jumper cables or are going to solder, you can do that without any problem.
- Pins for interrupts and synchronization: useful if you are going to take advantage of the advanced modes of operation.
- I²C Boosting: You can configure up to four addresses, so if you want to have several sensors at once, you won't have a problem either.
The 3,3V power supply is standard on the SparkFun board, although you can adapt the power supply according to your microcontroller's requirements. If you use Arduino Uno or Mega, it is enough to use a logic level converter if necessary, although most modern boards compatible with Qwiic already work directly at 3,3 V.
Arduino Library for the AS7331: How to Install It and What It Does
The SparkFun team has developed a specific library for Arduino which greatly simplifies working with this sensor. You can find it directly in the Arduino Library Manager by searching for "SparkFun AS7331" or download it from the GitHub repository. This library not only allows you to initialize and read data from all three UV channels, but also includes methods for:
- Adjust the sensor gain to improve sensitivity or dynamic range depending on the incident light.
- Modify operating modes (select between the four available with just one line of code).
- Change the I²C address from the software itself, as long as you have physically configured the corresponding jumpers.
- Check the low power status and the speed of the sensor's internal clock.
- Access data from each UV channel independently or in combination, which allows the calculation of the UV index or more complex spectral analyses.
Practical examples of use with Arduino
Let's review how you can start working with the AS7331 and Arduino with two very typical cases, thanks to the examples included in the official library:
Example 1: Basic Metering (One-Shot Mode)
This first example is the ideal starting point. Use single measurement mode, that is, the sensor remains in standby mode until we request a measurement, which it does and automatically returns to low-power mode. This way, you can record specific UV radiation values with virtually no energy consumption.
The flow would be like this:
- Import the library and create the sensor object.
- Initialize I²C communication.
- Run measurement whenever you want (for example, every so often with a timer, or on demand by pressing a button).
- Obtain data from all three channels (UVA, UVB, UVC) and display it on the serial monitor or use it to calculate the UV index.
The best thing about this method is its low power consumption and simplicity. As soon as you load the sketch, you'll see the data updated every 2 seconds on the serial monitor.
Example 2: Continuous mode measurement
For when you need real-time data without pauses, the library allows you to switch to the continuous modeHere, the sensor is constantly taking measurements and sending data, which is very useful in environmental monitoring systems where the rate of change is rapid.
In this case, you just have to connect the INT pin to an Arduino digital pin capable of handling interrupts (by default they usually use D26, but you can change it in the code).
You can use the serial monitor in exactly the same way to display the constant UV data stream.
Other advanced usage modes
If your needs go further, the AS7331's synchronized modes (SYNS and SYND) allow you to synchronize the sensor with external processes, control the start and end of each measurement from the microcontroller, and even monitor the exact acquisition time (clock-level timing control). These types of features are exceptional for sensors of this price and size range.
Tips to get the most out of the AS7331 with Arduino
- Use the Qwiic system If you are looking for ease of connection and want to avoid typical wiring errors.
- Take advantage of the possibility of multiple sensors on the same I²C bus modifying the directions, perfect for comparing radiation at different locations or angles.
- In battery-powered projects, prioritize low-power modes., like the One-shot, and disconnects the sensor when measurement is not essential.
- Always refer to the data sheet and examples in the library, as there you will find details about internal logs and how to get the most out of their capabilities.
- Remember that UVA, UVB and UVC channels can have different uses: UVA is linked to long-term skin damage, UVB to sunburn, and UVC is especially important in disinfection and environmental control applications.
Access to documentation, resources and support
One of the strengths of the AS7331/SparkFun ecosystem is the extensive documentation and community that supports it. From the SparkFun website you can download:
- Schematics and Eagle files to modify or integrate your own hardware.
- Step-by-step connection guides with clear images and descriptions.
- The sensor datasheet (AS7331) in detail.
- Links to hardware and software repositories on GitHub, which facilitate both integration with Arduino and adaptation to other environments.
Don't forget that the open source community is very active. And on platforms like GitHub, you can find updates, patches, improvements, or even library variants tailored to different boards or needs. And if you're more into more advanced DIY, you can always contribute your own examples or improvements.
Comparison with other popular UV sensors
If you've checked the market, you'll see plenty of alternatives like Adafruit's LTR390 or simple UV sensors based on photodiodes, but The AS7331 excels in having true independent spectral channels, very wide dynamic range and advanced control options.Many other sensors barely distinguish UVA from UVB or don't even offer easy integration with Arduino, while the AS7331 puts it all at your fingertips in a compact and rugged form factor.
Where can I get AS7331 and how much does it cost?
The AS7331 is currently available from specialty retailers such as SparkFun, OpenCircuit, and major e-commerce platforms such as Amazon and AliExpress. Its price may vary depending on the distributor and whether you buy the complete Qwiic board or just the sensor, but it is usually around 20 to 40 euros.It's not the cheapest sensor, but its price-performance ratio and the possibilities it offers more than justify it.
Practical applications and project ideas
With the AS7331, you can do much more than just measure the sun. Here are some ideas:
- Portable UV Monitor: for athletes, hikers or outdoor workers, to warn when exposure is dangerous.
- Advanced Home Weather Station: Combining UV data with temperature, humidity and pressure for comprehensive environmental studies.
- Industrial quality control: ensuring that UV curing processes (such as adhesives or varnishes) receive the exact dose.
- UVC disinfection systems: monitoring the amount of germicidal light in hospitals, laboratories or even home purifiers.
- Educational projects: to teach practical concepts about electromagnetic radiation, sun protection or the physics of materials.
The only limit is your imagination and the needs of your project!
The AS7331 sensor represents the latest in affordable UV sensing for makers and professionals.Its ease of integration, powerful Arduino library, and open source community support make it a perfect tool for both learning and tackling complex challenges. Whether you're looking to measure accurately, experiment, or develop advanced applications, you won't be disappointed.