Hello

Welcome lekule blog

Hi, I`m Sostenes, Electrical Technician and PLC`S Programmer.
Everyday I`m exploring the world of Electrical to find better solution for Automation.
together in the world. #lekule86
Join us on

Using Scilab GUI for RGB and Lux Measurements

Gather data via USB from a BH1745NUC optical sensor and then display color characteristics and illuminance values.

Supporting Information

  • This article introduces Scilab.
  • Two previous articles provide information on incorporating USB communication into an EFM8 project: Communicating with an EFM8 Microcontroller via USB and EFM8 Sound Synthesizer: Playing Melodies via USB.
  • This project is an extension of the functionality described in Design a Color Sensor with Measurements Displayed via an RGB LED Module, Part 1 and Part 2. That project as well as the project described in this article make use of a custom-designed PCB; please refer to Custom PCB Design with an EFM8 Microcontroller for guidance on incorporating EFM8 microcontrollers into your custom hardware.

A Handy Optical-Analysis System

The goal of this project is to turn our EFM8-plus-BH1745NUC circuit into a USB-based system for monitoring and analyzing the color characteristics and illuminance of whatever light is illuminating the sensor. You can use this system to determine the RGB content generated by any light source, but the intended purpose is the general evaluation of ambient illumination. This system provides information on the two most important aspects of ambient lighting: illuminance, which could be described as the “effective brightness” (more details here), and spectral composition, which influences a person’s physiological and psychological response to lighting conditions (more details on spectral composition here, under “The Colors of White”).

The GUI

This is the graphical user interface we will be using for this project:

The GUI runs in Scilab and was designed with the help of the GUI Builder toolbox, which you can download through Scilab’s ATOMS module manager:

You can use the following link to download the GUI script (it’s just a single text file).

 

First you use the “Open VCP Port” button to establish a virtual COM port connection to the EFM8 microcontroller. Next, choose the desired interval between data requests; 5 seconds is the minimum. After you click “Begin RGBC Data Collection,” the GUI repeatedly requests data until you click “Stop RGBC Data Collection.” The vertical sliders and the small colored rectangle convey RGB data for the most recent measurement, and the plot shows all the lux values since the beginning of the current data collection period; to clear the plot and begin a new lux record, click “Stop RGBC Data Collection” then “Begin RGBC Data Collection.”
The plotting functionality is useful for tracking changes in illuminance—for example, you could record how much illuminance is provided by windows throughout the course of a day, and this information could help you to determine an appropriate schedule for artificial lighting. (The variations in the plot shown above reflect changes in indoor illuminance caused by “partially cloudy” weather conditions.) The sliders and the rectangle are two different ways of displaying color information: the sliders allow you to accurately assess the proportion of red, green, and blue light in the incident radiation, whereas the rectangle combines the red, green, and blue proportions into a single color that represents which wavelengths are dominant.

Conveying Color

Let’s take a look at some examples of how the GUI reports RGB information for different light sources. The following spectral composition plots (available here) from the National Institute of Standards and Technology remind us of what we should expect from sunlight, incandescent bulbs, and white LEDs; the peak sensitivity wavelength of our R, G, and B detectors is about 625 nm, 535 nm, and 455 nm, respectively.

Here are results for indirect sunlight shining through a window. Notice that blue and green are dominant with significantly less red, resulting in a greenish-bluish rectangle. This is consistent with the general spectral trend shown in the above plot.

Next we have results for incandescent lighting. Notice how the rectangle immediately conveys the dominance of longer (i.e., yellow/orange/red) wavelengths. The sliders also aptly represent how the intensity of the incandescent spectrum increases with wavelength.

Lastly, we have data for light from a white-LED flashlight. White LEDs are known for their higher portion of blue wavelengths, and that is exactly what we see in the colored rectangle and the sliders.

Note: The sliders operate on an absolute scale. The default maximum value for each slider is 5000. This works well for indoor measurements, but the intensities will easily exceed this in outdoor or other high-brightness situations. The following code excerpt shows where to adjust the maximum values:


In Search of Lux

It is not particularly easy to perform accurate illuminance measurements, a fact of which you are well aware if you have read my three previous articles on this topic (Understanding Illuminance: What’s in a Lux? and Measuring and Calculating Lux Values, Part 1 and Part 2). The GUI developed for this project follows the calculation procedure discussed in Measuring and Calculating Lux Values, Part 2. Here is my Scilab-script implementation of this procedure:

Below is the code I use to create the colored rectangle. The scaling technique is discussed in Design a Color Sensor with Measurements Displayed via an RGB LED Module, Part 2; I’ll repeat the salient sentences for your convenience:
Our goal here is to “measure” color, regardless of the overall intensity of the light illuminating the photodetectors. Thus, we need to scale the RGB values in a way that standardizes the absolute value of the measurements while preserving the relative value—in other words, we maximize the overall intensity while maintaining the proportion of red, green, and blue in the incident light. To accomplish this, we multiply the highest of the three measurements by whatever factor increases this highest measurement to the maximum value, then we multiply the other two measurements by the same factor.
 

Important Note: The datasheet for the BH1745NUC indicates that the sensitivity specs given in the “Electrical Characteristics” table are based on measurements performed with the sensor’s internal gain set to 16x. I believe that this is an error; I calculated the sensitivity assuming that the gain was 1x. An error of this kind would not be surprising in view of the overall quality of the datasheet; actually, one of the other specifications in this table appears to include a similar mistake. Furthermore, most of my lux measurements indicate that the 1x-based sensitivity is correct. I performed two experiments, one with incandescent bulbs and one with a candle, and in both cases my lux values were consistent with theoretical predictions—certainly not off by a factor of 16. However, my lux values for full-sun outdoor conditions seemed too low, though increasing these values by a factor of 16 would make them too high. The low outdoor lux values could be related to some sort of saturation or desensitization that occurs at very high light intensities—the datasheet indicates that the dynamic range of the sensor extends only to 40,000 lux, whereas direct sunlight can exceed 100,000 lux. Bottom line, more experimentation is needed to determine how to optimize a BH1745NUC-based lux meter.

Firmware

Here is a link to download the firmware.

 

Please refer to Design a Color Sensor with Measurements Displayed via an RGB LED Module, Part 1 and Part 2, for information on the firmware used in this project. Most of the USB functionality required for communicating with Scilab is in the “USBInterface.c” source file:

                    uint16_t xdata USBBytesRcvd = 0;          //holds size of received packet
uint16_t xdata USBBytesTransmitted = 0;    //holds number of bytes actually transmitted
uint8_t xdata USBRxPacket[USB_PACKET_SIZE];  //packet received from host

bit REQUEST_RGBC_DATA = FALSE;
bit REQUEST_TEMP_DATA = FALSE;


/******************************************************************************
 * VCPXpress callback
 * This function is called by VCPXpress.
 *****************************************************************************/
VCPXpress_API_CALLBACK(myAPICallback)
{
   uint32_t API_InterruptCode;

   //get the code that indicates the reason for the interrupt
   API_InterruptCode = Get_Callback_Source();

   //if the USB connection was just opened
   if (API_InterruptCode & DEVICE_OPEN)
   {
    //start the first USB read procedure
    Block_Read(USBRxPacket, USB_PACKET_SIZE, &USBBytesRcvd);
    /*we will process the received bytes when we get
    a callback with an RX_COMPLETE interrupt code*/
   }

   if (API_InterruptCode & RX_COMPLETE)   //USB read complete
   {
    //confirm that the received packet is the proper length
    if(USBBytesRcvd == USB_CMD_LEN)
    {
     //'R' indicates that the host is Requesting data
     if(USBRxPacket[0] == 'R')
     {
      //is the host requesting data from the RGBC sensor?
      if(USBRxPacket[2] == 'R' && USBRxPacket[3] == 'G' && USBRxPacket[4] == 'B' && USBRxPacket[5] == 'C')
      {
       REQUEST_RGBC_DATA = TRUE;
      }

      //is the host requesting data from the thermocouple IC?
      else if(USBRxPacket[2] == 'T' && USBRxPacket[3] == 'E' && USBRxPacket[4] == 'M' && USBRxPacket[5] == 'P')
      {
       REQUEST_TEMP_DATA = TRUE;
      }
     }
    }

    //continue with the next USB read procedure
    Block_Read(USBRxPacket, USB_PACKET_SIZE, &USBBytesRcvd);
   }
}

void TransmitUSB_TempData()
{
    Block_Write(TempDataTx, TEMP_DATA_LEN, &USBBytesTransmitted);
}

void TransmitUSB_RGBCData()
{
    Block_Write(I2C_RcvData, RGBC_DATA_LEN, &USBBytesTransmitted);
}

                  

You will notice that the USB code includes a request for temperature data in addition to a request for RGBC data. This gives you an example of how to incorporate additional commands into the USB interface (the temperature-measurement functionality will be used in a future project).

Share this:

ABOUTME

Hi all. This is deepak from Bthemez. We're providing content for Bold site and we’ve been in internet, social media and affiliate for too long time and its my profession. We are web designer & developer living India! What can I say, we are the best..

Post a Comment
My photo

Hi, I`m Sostenes, Electrical Technician and PLC`S Programmer.
Everyday I`m exploring the world of Electrical to find better solution for Automation. I believe everyday can become a Electrician with the right learning materials.
My goal with BLOG is to help you learn Electrical.

Labels

LEKULE TV EDITORIALS ARTICLES DC ROBOTICS DIGITAL SEMICONDUCTORS GENERATOR AC EXPERIMENTS MANUFACTURING-ENGINEERING REFERENCE FUNDAMENTAL OF ELECTRICITY ELECTRONICS ELECTRICAL ENGINEER MEASUREMENT TRANSDUCER & SENSOR VIDEO ARDUINO RENEWABLE ENERGY AUTOMOBILE TEARDOWN SYNCHRONOUS GENERATOR DIGITAL ELECTRONICS ELECTRICAL DISTRIBUTION CABLES AUTOMOTIVE MICROCONTROLLER SOLAR PROTECTION DIODE AND CIRCUITS BASIC ELECTRICAL ELECTRONICS MOTOR SWITCHES CIRCUIT BREAKERS CIRCUITS THEORY PANEL BUILDING ELECTRONICS DEVICES MIRACLES SWITCHGEAR ANALOG MOBILE DEVICES WEARABLES CAMERA TECHNOLOGY COMMUNICATION GENERATION BATTERIES FREE CIRCUITS INDUSTRIAL AUTOMATION SPECIAL MACHINES ELECTRICAL SAFETY ENERGY EFFIDIENCY-BUILDING DRONE CONTROL SYSTEM NUCLEAR ENERGY SMATRPHONE FILTER`S POWER BIOGAS BELT CONVEYOR MATERIAL HANDLING RELAY ELECTRICAL INSTRUMENTS ENERGY SOURCE PLC`S TRANSFORMER AC CIRCUITS CIRCUIT SCHEMATIC SYMBOLS DDISCRETE SEMICONDUCTOR CIRCUITS WIND POWER C.B DEVICES DC CIRCUITS DIODES AND RECTIFIERS FUSE SPECIAL TRANSFORMER THERMAL POWER PLANT CELL CHEMISTRY EARTHING SYSTEM ELECTRIC LAMP FUNDAMENTAL OF ELECTRICITY 2 BIPOLAR JUNCTION TRANSISTOR 555 TIMER CIRCUITS AUTOCAD BLUETOOTH C PROGRAMMING HOME AUTOMATION HYDRO POWER LOGIC GATES OPERATIONAL AMPLIFIER`S SOLID-STATE DEVICE THEORRY COMPUTER DEFECE & MILITARY FLUORESCENT LAMP INDUSTRIAL ROBOTICS ANDROID ELECTRICAL DRIVES GROUNDING SYSTEM CALCULUS REFERENCE DC METERING CIRCUITS DC NETWORK ANALYSIS ELECTRICAL SAFETY TIPS ELECTRICIAN SCHOOL ELECTRON TUBES FUNDAMENTAL OF ELECTRICITY 1 INDUCTION MACHINES INSULATIONS USB ALGEBRA REFERENCE HMI[Human Interface Machines] INDUCTION MOTOR KARNAUGH MAPPING USEUL EQUIATIONS AND CONVERSION FACTOR ANALOG INTEGRATED CIRCUITS BASIC CONCEPTS AND TEST EQUIPMENTS DIGITAL COMMUNICATION DIGITAL-ANALOG CONVERSION ELECTRICAL SOFTWARE GAS TURBINE ILLUMINATION OHM`S LAW POWER ELECTRONICS THYRISTOR BOOLEAN ALGEBRA DIGITAL INTEGRATED CIRCUITS FUNDAMENTAL OF ELECTRICITY 3 PHYSICS OF CONDUCTORS AND INSULATORS SPECIAL MOTOR STEAM POWER PLANTS TESTING TRANSMISION LINE C-BISCUIT CAPACITORS COMBINATION LOGIC FUNCTION COMPLEX NUMBERS CONTROL MOTION ELECTRICAL LAWS INVERTER LADDER DIAGRAM MULTIVIBRATORS RC AND L/R TIME CONSTANTS SCADA SERIES AND PARALLEL CIRCUITS USING THE SPICE CIRCUIT SIMULATION PROGRAM AMPLIFIERS AND ACTIVE DEVICES APPS & SOFTWARE BASIC CONCEPTS OF ELECTRICITY CONDUCTOR AND INSULATORS TABLES CONDUITS FITTING AND SUPPORTS ELECTRICAL INSTRUMENTATION SIGNALS ELECTRICAL TOOLS INDUCTORS LiDAR MAGNETISM AND ELECTROMAGNETISM PLYPHASE AC CIRCUITS RECLOSER SAFE LIVING WITH GAS AND LPG SAFETY CLOTHING STEPPER MOTOR SYNCHRONOUS MOTOR AC METRING CIRCUITS BECOME AN ELECTRICIAN BINARY ARITHMETIC BUSHING DIGITAL STORAGE MEMROY ELECTRICIAN JOBS HEAT ENGINES HOME THEATER INPECTIONS LIGHT SABER MOSFET NUMERATION SYSTEM POWER FACTORS REACTANCE AND IMPEDANCE INDUCTIVE RECTIFIER AND CONVERTERS RESONANCE SCIENTIFIC NOTATION AND METRIC PREFIXES SULFURIC ACID TROUBLESHOOTING TROUBLESHOOTING-THEORY & PRACTICE 12C BUS APPLE BATTERIES AND POWER SYSTEMS DC MOTOR DRIVES ELECTROMECHANICAL RELAYS ENERGY EFFICIENCY-LIGHT INDUSTRIAL SAFETY EQUIPMENTS MEGGER MXED-FREQUENCY AC SIGNALS PRINCIPLE OF DIGITAL COMPUTING QUESTIONS REACTANCE AND IMPEDANCE-CAPATIVE SEQUENTIAL CIRCUITS SERRIES-PARALLEL COMBINATION CIRCUITS SHIFT REGISTERS WIRELESS BUILDING SERVICES COMPRESSOR CRANES DIVIDER CIRCUIT AND KIRCHHOFF`S LAW ELECTRICAL DISTRIBUTION EQUIPMENTS 1 ELECTRICAL DISTRIBUTION EQUIPMENTS B ELECTRICAL TOOL KIT ELECTRICIAN JOB DESCRIPTION INDUSTRIAL DRIVES LAPTOP SCIENCE THERMOCOUPLE TRIGONOMENTRY REFERENCE UART oscilloscope BIOMASS CONTACTOR ELECTRIC ILLUMINATION ELECTRICAL SAFETY TRAINING ELECTROMECHANICAL FEATURED FILTER DESIGN HARDWARE JUNCTION FIELD-EFFECT TRANSISTORS NASA NUCLEAR POWER VALVE COLOR CODES ELECTRIC TRACTION FLEXIBLE ELECTRONICS FLUKE GEARMOTORS INTRODUCTION LASSER PID PUMP SEAL ELECTRICIAN CAREER ELECTRICITY SUPPLY AND DISTRIBUTION MUSIC NEUTRAL PERIODIC TABLES OF THE ELEMENTS POLYPHASE AC CIRCUITS PROJECTS REATORS SATELLITE STAR DELTA VIBRATION WATERPROOF