A concept design laboratory prototype for measuring ambient light value. The high levels typically mean that there is high rates of UVA and UVB radiation as well. A warning of this is displayed on a small screen. NXP FRDM-K64F, Digilent Pmod modules Pmod OLEDrgb and Pmod ALS. Tested as well on ST L432KC.

Dependencies:   Adafruit_SSD1331_Mbed Adafruit-GFX

Committer:
timo_k2
Date:
Mon Sep 28 07:32:51 2020 +0000
Revision:
2:0eed70300cb4
Parent:
1:fde67a85d481
Code republished. No essential changes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 0:87339e72a5eb 1 /* mbed Microcontroller Library
timo_k2 0:87339e72a5eb 2 * Copyright (c) 2019 ARM Limited
timo_k2 0:87339e72a5eb 3 * SPDX-License-Identifier: Apache-2.0
timo_k2 1:fde67a85d481 4 ************************************************************************
timo_k2 1:fde67a85d481 5 *
timo_k2 1:fde67a85d481 6 * Test of the Pmod small OLED display and ambient light sensor
timo_k2 1:fde67a85d481 7 *
timo_k2 1:fde67a85d481 8 *************************************************************************
timo_k2 1:fde67a85d481 9 * Description: Pmod_OLED_ALS1
timo_k2 1:fde67a85d481 10 * The message "Test module Pmod Digilent Lextronic" will be display on OLEDrgb module
timo_k2 1:fde67a85d481 11 * with different size and colors..... and sensor value as well.
timo_k2 1:fde67a85d481 12 *
timo_k2 1:fde67a85d481 13 * Material
timo_k2 1:fde67a85d481 14 * 1. ST L432KC ( or some other micro controller board with SPI communication )
timo_k2 1:fde67a85d481 15 * 2. Digilent Pmod OLEDrgb and the libraries
timo_k2 1:fde67a85d481 16 * Adafruit_SSD1331_Mbed
timo_k2 1:fde67a85d481 17 * Adafruit-GFX
timo_k2 1:fde67a85d481 18 * These libraries can be found with search on the page
timo_k2 1:fde67a85d481 19 * https://www.mbed.com/en/platform/mbed-os/
timo_k2 1:fde67a85d481 20 * Please connect L432KC - Pmod_OLEDrgb with lines:
timo_k2 1:fde67a85d481 21 * L432KC D13 - OLED 4 SCK hardware defined for the SPI
timo_k2 1:fde67a85d481 22 * L432KC D11 - OLED 2 MOSI hardware definef for the SPI
timo_k2 1:fde67a85d481 23 * L432KC D9 - OLED 1 CS or any other free
timo_k2 1:fde67a85d481 24 * L432KC D10 - OLED 7 DC or any other free
timo_k2 1:fde67a85d481 25 * L432KC D6 - OLED 8 RES or any other free
timo_k2 1:fde67a85d481 26 * VCC - OLED 9 VCCEN Enable - VCC
timo_k2 1:fde67a85d481 27 * VCC - OLED10 PMODEN Pmod Enable - VCC
timo_k2 1:fde67a85d481 28 * GND - OLED 5 Ground
timo_k2 1:fde67a85d481 29 * VCC - OLED 6 Power supply 3,3 V
timo_k2 1:fde67a85d481 30 *
timo_k2 1:fde67a85d481 31 *************************************************************************
timo_k2 1:fde67a85d481 32 * 3. Digilent Pmod ALS ambient light sensor
timo_k2 1:fde67a85d481 33 * Please connect L432KC - PmodALS with lines:
timo_k2 1:fde67a85d481 34 * L432KC D13 - ALS 4 SCK hardware defined for the SPI
timo_k2 1:fde67a85d481 35 * L432KC D12 - ALS 3 MISO hardware defined for the SPI
timo_k2 1:fde67a85d481 36 * L432KC D4 - ALS 1 CS or any other free
timo_k2 1:fde67a85d481 37 * GND - ALS 5 GND
timo_k2 1:fde67a85d481 38 * Vcc - ALS 6 Vcc
timo_k2 1:fde67a85d481 39
timo_k2 1:fde67a85d481 40 ALS data on the SPI
timo_k2 1:fde67a85d481 41 D15 ... D13 - three zeros
timo_k2 1:fde67a85d481 42 D12 ... D04 - 8 bits of ambient light data
timo_k2 1:fde67a85d481 43 D03 ... D00 - four zeros
timo_k2 1:fde67a85d481 44
timo_k2 1:fde67a85d481 45 Details on the connection of ADC IC chip on ALS board are given
timo_k2 1:fde67a85d481 46 http://www.ti.com/lit/ds/symlink/adc081s021.pdf
timo_k2 1:fde67a85d481 47 *
timo_k2 2:0eed70300cb4 48 * Timo Karppinen 16.05.2020 Apache-2.0
timo_k2 1:fde67a85d481 49 **************************************************************/
timo_k2 1:fde67a85d481 50
timo_k2 0:87339e72a5eb 51
timo_k2 0:87339e72a5eb 52 #include "mbed.h"
timo_k2 1:fde67a85d481 53 #include "Adafruit_SSD1331.h" // By using the Adafruit SSD1331 library and Adafruit GFX library
timo_k2 1:fde67a85d481 54 #include "Adafruit_GFX.h" // we will get similar code than in Arduino-boards working.
timo_k2 1:fde67a85d481 55 // There are other SSD1331 libraries, too. https://os.mbed.com/search/?q=ssd1331
timo_k2 1:fde67a85d481 56
timo_k2 0:87339e72a5eb 57 // PmodOLEDrgb
timo_k2 1:fde67a85d481 58 Adafruit_SSD1331 OLED(D9, D6, D10, D11, NC, D13); // cs, res, dc, mosi, (nc), sck
timo_k2 0:87339e72a5eb 59
timo_k2 1:fde67a85d481 60 //DigitalOut LED(LED1); // LED1, LED2, LED3 and LED4 are the D13 PB_3 pin in this board and can not be used because of the SPI
timo_k2 1:fde67a85d481 61 DigitalOut VCCEN(D3);
timo_k2 0:87339e72a5eb 62 DigitalOut PMODEN(D5);
timo_k2 0:87339e72a5eb 63
timo_k2 0:87339e72a5eb 64 // Definition of colors on the OLED display
timo_k2 0:87339e72a5eb 65 #define Black 0x0000
timo_k2 0:87339e72a5eb 66 #define Blue 0x001F
timo_k2 0:87339e72a5eb 67 #define Red 0xF800
timo_k2 0:87339e72a5eb 68 #define Green 0x07E0
timo_k2 0:87339e72a5eb 69 #define Cyan 0x07FF
timo_k2 0:87339e72a5eb 70 #define Magenta 0xF81F
timo_k2 0:87339e72a5eb 71 #define Yellow 0xFFE0
timo_k2 0:87339e72a5eb 72 #define White 0xFFFF
timo_k2 0:87339e72a5eb 73
timo_k2 0:87339e72a5eb 74 // PmodALS
timo_k2 0:87339e72a5eb 75 SPI spi(D11, D12, D13); // mosi, miso, sck
timo_k2 0:87339e72a5eb 76
timo_k2 0:87339e72a5eb 77
timo_k2 0:87339e72a5eb 78 DigitalOut alsCS(D4); // chip select for sensor SPI communication
timo_k2 0:87339e72a5eb 79 char alsByte0 = 0; // 8 bit data from sensor board, char is the unsigned 8 bit
timo_k2 0:87339e72a5eb 80 char alsByte1 = 0; // 8 bit data from sensor board
timo_k2 0:87339e72a5eb 81 char alsByteSh0 = 0;
timo_k2 0:87339e72a5eb 82 char alsByteSh1 = 0;
timo_k2 0:87339e72a5eb 83 char als8bit = 0;
timo_k2 0:87339e72a5eb 84 unsigned short alsRaw = 0; // unsigned 16 bit
timo_k2 0:87339e72a5eb 85 float alsScaledF = 0; // 32 bit floating point
timo_k2 0:87339e72a5eb 86 void getALS();
timo_k2 0:87339e72a5eb 87
timo_k2 0:87339e72a5eb 88
timo_k2 0:87339e72a5eb 89 char Time[50],Date[50];
timo_k2 0:87339e72a5eb 90 void getTime();
timo_k2 1:fde67a85d481 91 int first = 0;
timo_k2 0:87339e72a5eb 92
timo_k2 0:87339e72a5eb 93 int main()
timo_k2 0:87339e72a5eb 94 {
timo_k2 0:87339e72a5eb 95 // Showing with a LED that program has started
timo_k2 1:fde67a85d481 96 // LED = 0;
timo_k2 0:87339e72a5eb 97 VCCEN = 1; // if you did not connect VCCEN permanently to Vcc
timo_k2 0:87339e72a5eb 98 PMODEN = 1; // if you did not connect PMODEN permanently to Vcc
timo_k2 1:fde67a85d481 99 ThisThread::sleep_for(2000);
timo_k2 1:fde67a85d481 100 // LED = 1;
timo_k2 1:fde67a85d481 101 ThisThread::sleep_for(2000);
timo_k2 1:fde67a85d481 102 // LED = 0;
timo_k2 0:87339e72a5eb 103 // Initalize the PmodOLEDrgb
timo_k2 1:fde67a85d481 104 OLED.begin(); // initialization of display object
timo_k2 0:87339e72a5eb 105
timo_k2 0:87339e72a5eb 106 // SPI for the ALS
timo_k2 0:87339e72a5eb 107 alsCS = 1;
timo_k2 0:87339e72a5eb 108 // Setup the spi for 8 bit data, high steady state clock,
timo_k2 0:87339e72a5eb 109 // second edge capture, with a 12MHz clock rate
timo_k2 0:87339e72a5eb 110 spi.format(8,0);
timo_k2 1:fde67a85d481 111 spi.frequency(12000000);
timo_k2 1:fde67a85d481 112
timo_k2 1:fde67a85d481 113 OLED.clearScreen();
timo_k2 0:87339e72a5eb 114
timo_k2 0:87339e72a5eb 115 while (true) {
timo_k2 0:87339e72a5eb 116 while(first < 3)
timo_k2 0:87339e72a5eb 117 {
timo_k2 0:87339e72a5eb 118 first += 1;
timo_k2 0:87339e72a5eb 119 OLED.fillScreen(Black); // background screen in black
timo_k2 0:87339e72a5eb 120 OLED.setTextColor(Cyan); // color of text in cyan
timo_k2 0:87339e72a5eb 121 OLED.setCursor(0,0); // cursor is in x=0 and y=15
timo_k2 0:87339e72a5eb 122 OLED.printf("Test module Pmod"); // display text
timo_k2 1:fde67a85d481 123 ThisThread::sleep_for(500); // wait 500 ms
timo_k2 0:87339e72a5eb 124 OLED.setCursor(0,15); // cursor is in x=0 and y=15
timo_k2 0:87339e72a5eb 125 OLED.setTextSize(2); // size of text
timo_k2 0:87339e72a5eb 126 OLED.setTextColor(Red); // text in red color
timo_k2 0:87339e72a5eb 127 OLED.printf("DIGILENT"); // display text
timo_k2 0:87339e72a5eb 128 OLED.setCursor(20,40); // cursor is in x=20 and y=40
timo_k2 0:87339e72a5eb 129 OLED.setTextSize(1); // size of text
timo_k2 0:87339e72a5eb 130 OLED.setTextColor(Green); // text in green color
timo_k2 0:87339e72a5eb 131 OLED.printf("LEXTRONIC"); // display text
timo_k2 0:87339e72a5eb 132 OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); // blue line x=1 to screen width-1 and y=60
timo_k2 1:fde67a85d481 133 ThisThread::sleep_for(2000); // wait 2 s
timo_k2 0:87339e72a5eb 134 OLED.fillScreen(Black); // background display in black (erase display)
timo_k2 0:87339e72a5eb 135 OLED.fillRoundRect(5, 5, 30, 40, 1, Blue); // French flag bleu blanc rouge
timo_k2 0:87339e72a5eb 136 OLED.fillRoundRect(35, 5, 30, 40, 1, White);
timo_k2 0:87339e72a5eb 137 OLED.fillRoundRect(65, 5, 30, 40, 1, Red);
timo_k2 0:87339e72a5eb 138 OLED.fillCircle(90, 55, 5, Yellow); // yellow circle with radius=5 in x=90 and y=55
timo_k2 1:fde67a85d481 139 ThisThread::sleep_for(2000); // wait 2 s
timo_k2 0:87339e72a5eb 140 }
timo_k2 0:87339e72a5eb 141
timo_k2 0:87339e72a5eb 142 getTime();
timo_k2 1:fde67a85d481 143 OLED.clearScreen();
timo_k2 0:87339e72a5eb 144 OLED.fillScreen(Black); // background screen in black
timo_k2 0:87339e72a5eb 145 OLED.setTextColor(Cyan); // color of text in cyan
timo_k2 1:fde67a85d481 146 ThisThread::sleep_for(300);
timo_k2 0:87339e72a5eb 147 OLED.setCursor(0,0); // cursor is in x=0 and y=0
timo_k2 1:fde67a85d481 148 OLED.printf("Rec '%s' \n",Time);
timo_k2 0:87339e72a5eb 149
timo_k2 0:87339e72a5eb 150 getALS();
timo_k2 1:fde67a85d481 151 OLED.printf("LUX = '%0.1f' \n",alsScaledF);
timo_k2 0:87339e72a5eb 152
timo_k2 0:87339e72a5eb 153 if (alsScaledF > 100)
timo_k2 0:87339e72a5eb 154 {
timo_k2 0:87339e72a5eb 155 OLED.setTextColor(Yellow);
timo_k2 0:87339e72a5eb 156 OLED.printf("Be aware of high UV radiation!");
timo_k2 0:87339e72a5eb 157 OLED.fillRoundRect(10, 35, 50, 3 , 1, Yellow);
timo_k2 0:87339e72a5eb 158 }
timo_k2 0:87339e72a5eb 159 OLED.setCursor(0,40); // cursor is in x=0 and y=0
timo_k2 0:87339e72a5eb 160 OLED.printf("Test completed");
timo_k2 1:fde67a85d481 161 OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); // blue line x=1 to screen width-1 and y=60
timo_k2 0:87339e72a5eb 162 printf("printed on OLED");
timo_k2 0:87339e72a5eb 163
timo_k2 1:fde67a85d481 164 ThisThread::sleep_for(3000);
timo_k2 0:87339e72a5eb 165 }
timo_k2 0:87339e72a5eb 166 }
timo_k2 0:87339e72a5eb 167
timo_k2 0:87339e72a5eb 168 void getTime()
timo_k2 0:87339e72a5eb 169 {
timo_k2 0:87339e72a5eb 170 time_t seconds = time(NULL);
timo_k2 0:87339e72a5eb 171 strftime(Time,40,"%H:%M:%S", localtime(&seconds));
timo_k2 0:87339e72a5eb 172 strftime(Date,40,"%d-%b-%Y", localtime(&seconds));
timo_k2 0:87339e72a5eb 173 printf("Recorded '%s' \r\n",Time);
timo_k2 0:87339e72a5eb 174 }
timo_k2 0:87339e72a5eb 175
timo_k2 0:87339e72a5eb 176 void getALS()
timo_k2 0:87339e72a5eb 177 {
timo_k2 0:87339e72a5eb 178 alsCS = 0;
timo_k2 0:87339e72a5eb 179
timo_k2 0:87339e72a5eb 180 alsByte0 = spi.write(0x00);
timo_k2 0:87339e72a5eb 181 alsByte1 = spi.write(0x00);
timo_k2 0:87339e72a5eb 182
timo_k2 0:87339e72a5eb 183 alsCS = 1;
timo_k2 0:87339e72a5eb 184
timo_k2 0:87339e72a5eb 185 alsByteSh0 = alsByte0 << 4;
timo_k2 0:87339e72a5eb 186 alsByteSh1 = alsByte1 >> 4;
timo_k2 0:87339e72a5eb 187
timo_k2 0:87339e72a5eb 188 als8bit =( alsByteSh0 | alsByteSh1 );
timo_k2 0:87339e72a5eb 189
timo_k2 0:87339e72a5eb 190 alsRaw = als8bit; //
timo_k2 0:87339e72a5eb 191 alsScaledF = (float(alsRaw))*(float(6.68)); // The value 6.68 is 64 bit double precision floating point of type double.
timo_k2 0:87339e72a5eb 192 // Conversions to 32 bit floating point of type float.
timo_k2 0:87339e72a5eb 193 printf("Ambient light raw 8 bit 0...255 = '%d' \r\n",alsRaw);
timo_k2 0:87339e72a5eb 194 printf("Ambient light scaled to LUX = '%0.1f' \r\n",alsScaledF);
timo_k2 1:fde67a85d481 195 ThisThread::sleep_for(100);
timo_k2 0:87339e72a5eb 196 }
timo_k2 0:87339e72a5eb 197