The code is developed for the hardware NUCLEO-L432KC or any other board with SPI communication and the Digilent Pmod ALS1 ambient light sensor module. The purpose is to make students to understand the development process. They need to study the original analog to digital converter documentation to understand how the converter 16 bit output is converted to an illuminance value.

Committer:
timo_k2
Date:
Sun Jan 09 19:07:52 2022 +0000
Revision:
1:444f907aa5eb
Parent:
0:fb0f74ee7b70
Initial commit on Keil Studio.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 0:fb0f74ee7b70 1 /* mbed Microcontroller Library
timo_k2 0:fb0f74ee7b70 2 * Copyright (c) 2019 ARM Limited
timo_k2 0:fb0f74ee7b70 3 * SPDX-License-Identifier: Apache-2.0
timo_k2 0:fb0f74ee7b70 4 ************************************************************************
timo_k2 0:fb0f74ee7b70 5 *
timo_k2 0:fb0f74ee7b70 6 * Test of the Pmod ambient light sensor
timo_k2 0:fb0f74ee7b70 7 *
timo_k2 0:fb0f74ee7b70 8 *************************************************************************
timo_k2 0:fb0f74ee7b70 9 * Description: McLab08_SPI_Pmod_ALS1_OS6
timo_k2 0:fb0f74ee7b70 10 * The ambient light value will be read and converted to LUX.
timo_k2 0:fb0f74ee7b70 11 *
timo_k2 0:fb0f74ee7b70 12 * Material
timo_k2 0:fb0f74ee7b70 13 * 1. ST NUCLEO L432KC , NXP FRDM-K64F
timo_k2 0:fb0f74ee7b70 14 * or some other micro controller board with SPI communication
timo_k2 0:fb0f74ee7b70 15 * 2. Digilent Pmod ALS ambient light sensor
timo_k2 0:fb0f74ee7b70 16 * Please connect L432KC or FRDM-K64F - PmodALS with lines:
timo_k2 0:fb0f74ee7b70 17 * L432KC D13 - ALS 4 SCK hardware defined for the SPI
timo_k2 0:fb0f74ee7b70 18 * L432KC D12 - ALS 3 MISO hardware defined for the SPI
timo_k2 0:fb0f74ee7b70 19 * L432KC D4 - ALS 1 CS or any other free
timo_k2 0:fb0f74ee7b70 20 * GND - ALS 5 GND
timo_k2 0:fb0f74ee7b70 21 * Vcc - ALS 6 Vcc
timo_k2 0:fb0f74ee7b70 22
timo_k2 0:fb0f74ee7b70 23 * ALS data on the SPI
timo_k2 0:fb0f74ee7b70 24 * D15 ... D12 - four zeros
timo_k2 0:fb0f74ee7b70 25 * D11 ... D04 - 8 bits of ambient light data
timo_k2 0:fb0f74ee7b70 26 * D03 ... D00 - four zeros
timo_k2 0:fb0f74ee7b70 27 *
timo_k2 0:fb0f74ee7b70 28 * Details on the ADC IC on ALS board are given at
timo_k2 0:fb0f74ee7b70 29 * http://www.ti.com/lit/ds/symlink/adc081s021.pdf
timo_k2 0:fb0f74ee7b70 30 * The Pmod ALS
timo_k2 0:fb0f74ee7b70 31 * https://reference.digilentinc.com/reference/pmod/pmodals/start
timo_k2 0:fb0f74ee7b70 32 *
timo_k2 0:fb0f74ee7b70 33 * Timo Karppinen 17.11.2020
timo_k2 0:fb0f74ee7b70 34 **************************************************************/
timo_k2 1:444f907aa5eb 35 #include "ThisThread.h"
timo_k2 0:fb0f74ee7b70 36 #include "mbed.h"
timo_k2 0:fb0f74ee7b70 37
timo_k2 0:fb0f74ee7b70 38 DigitalOut LED(D1); // LD1 to LD4 pin names are linked to D13 in L432KC.
timo_k2 0:fb0f74ee7b70 39 // The board LD3 is the D13. Do not use. It is the SPISCK.
timo_k2 0:fb0f74ee7b70 40 // PmodALS
timo_k2 0:fb0f74ee7b70 41 SPI spi(D11, D12, D13); // mosi, miso, sck
timo_k2 0:fb0f74ee7b70 42
timo_k2 0:fb0f74ee7b70 43 DigitalOut alsCS(D4); // chip select for sensor SPI communication
timo_k2 0:fb0f74ee7b70 44
timo_k2 0:fb0f74ee7b70 45 int alsScaledI = 0; // 32 bit integer
timo_k2 0:fb0f74ee7b70 46 int getALS();
timo_k2 0:fb0f74ee7b70 47
timo_k2 0:fb0f74ee7b70 48 int main()
timo_k2 0:fb0f74ee7b70 49 {
timo_k2 0:fb0f74ee7b70 50 // SPI for the ALS
timo_k2 0:fb0f74ee7b70 51 // Setup the spi for 8 bit data, high steady state clock,
timo_k2 1:444f907aa5eb 52 // second edge capture, with a 2MHz clock rate
timo_k2 0:fb0f74ee7b70 53 spi.format(8,0);
timo_k2 1:444f907aa5eb 54 spi.frequency(2000000);
timo_k2 0:fb0f74ee7b70 55 // ready to wait the conversion start
timo_k2 0:fb0f74ee7b70 56 alsCS.write(1);
timo_k2 1:444f907aa5eb 57 ThisThread::sleep_for(1ms);
timo_k2 0:fb0f74ee7b70 58
timo_k2 0:fb0f74ee7b70 59 while (true) {
timo_k2 0:fb0f74ee7b70 60
timo_k2 0:fb0f74ee7b70 61 alsScaledI = getALS();
timo_k2 0:fb0f74ee7b70 62 printf("Ambient light scaled to LUX = '%0d' \r\n",alsScaledI);
timo_k2 0:fb0f74ee7b70 63
timo_k2 0:fb0f74ee7b70 64 if (alsScaledI > 100){
timo_k2 0:fb0f74ee7b70 65 LED.write(0);
timo_k2 0:fb0f74ee7b70 66 printf("Be aware of high UV radiation! \n\n");
timo_k2 0:fb0f74ee7b70 67 }
timo_k2 0:fb0f74ee7b70 68 else{
timo_k2 0:fb0f74ee7b70 69 LED.write(1);
timo_k2 0:fb0f74ee7b70 70 printf("Too low light for working \n\n");
timo_k2 0:fb0f74ee7b70 71 }
timo_k2 0:fb0f74ee7b70 72
timo_k2 0:fb0f74ee7b70 73 ThisThread::sleep_for(3000ms);
timo_k2 0:fb0f74ee7b70 74 }
timo_k2 0:fb0f74ee7b70 75 }
timo_k2 0:fb0f74ee7b70 76
timo_k2 0:fb0f74ee7b70 77 int getALS()
timo_k2 0:fb0f74ee7b70 78 {
timo_k2 0:fb0f74ee7b70 79 char alsByte0 = 0; //8bit data from sensor board, char is the unsigned 8bit
timo_k2 0:fb0f74ee7b70 80 char alsByte1 = 0; // 8bit data from sensor board
timo_k2 0:fb0f74ee7b70 81 char alsByteSh0 = 0;
timo_k2 0:fb0f74ee7b70 82 char alsByteSh1 = 0;
timo_k2 0:fb0f74ee7b70 83 char als8bit = 0;
timo_k2 0:fb0f74ee7b70 84 unsigned short alsRaw = 0; // unsigned 16 bit
timo_k2 0:fb0f74ee7b70 85 float alsScaledF = 0; // 32 bit floating point
timo_k2 0:fb0f74ee7b70 86
timo_k2 0:fb0f74ee7b70 87 // Begin the conversion process and serial data output
timo_k2 0:fb0f74ee7b70 88 alsCS.write(0);
timo_k2 0:fb0f74ee7b70 89 // Reading two 8bit bytes by writing two dymmy 8bit bytes
timo_k2 1:444f907aa5eb 90 ThisThread::sleep_for(1ms);
timo_k2 0:fb0f74ee7b70 91 alsByte0 = spi.write(0x00);
timo_k2 0:fb0f74ee7b70 92 alsByte1 = spi.write(0x00);
timo_k2 0:fb0f74ee7b70 93 // End of serial data output and back to tracking mode
timo_k2 0:fb0f74ee7b70 94 alsCS.write(1);
timo_k2 1:444f907aa5eb 95 ThisThread::sleep_for(1ms);
timo_k2 0:fb0f74ee7b70 96 // Check the http://www.ti.com/lit/ds/symlink/adc081s021.pdf
timo_k2 0:fb0f74ee7b70 97 // shifting bits to get the number out
timo_k2 0:fb0f74ee7b70 98 alsByteSh0 = alsByte0 << 4;
timo_k2 0:fb0f74ee7b70 99 alsByteSh1 = alsByte1 >> 4;
timo_k2 0:fb0f74ee7b70 100
timo_k2 0:fb0f74ee7b70 101 als8bit =( alsByteSh0 | alsByteSh1 );
timo_k2 0:fb0f74ee7b70 102
timo_k2 0:fb0f74ee7b70 103 alsRaw = als8bit;
timo_k2 0:fb0f74ee7b70 104 alsScaledF = (float(alsRaw))*(float(6.68));
timo_k2 0:fb0f74ee7b70 105 // The value 6.68 is 64 bit double precision floating point of type double.
timo_k2 0:fb0f74ee7b70 106 // Conversions to 32 bit floating point of type float.
timo_k2 0:fb0f74ee7b70 107
timo_k2 0:fb0f74ee7b70 108 printf("Ambient light raw 8 bit 0...255 = '%d' \r\n",alsRaw);
timo_k2 0:fb0f74ee7b70 109 //printf("Ambient light scaled to LUX = '%0.1f' \r\n",alsScaledF);
timo_k2 0:fb0f74ee7b70 110 //Sorry, no more float printing in OS6 !
timo_k2 0:fb0f74ee7b70 111 return (int)alsScaledF;
timo_k2 0:fb0f74ee7b70 112 }
timo_k2 0:fb0f74ee7b70 113