Panel Cookies
Spectrometer - Code
13/11/2022 | Views: 7710 | Arduino | by: ELECTRONOOBS      


If you open this code you will see that we first import the library for the display. Download it from the link below as well. Then we define the pins for the camera. In the void loop we first print the graph lines for the X axis. Then we use the “GET CAMERA” function. This function will create a “FOR” loop from 0 to 127 because we read 128 pixels from the camera. After each clock pulse, we read the analog input on A0 and we store the brightness value on this vector on the corresponding position from 0 to 127. Now we have all brightness values stores. In the void loop we create another “FOR” from 0 to 128 and we create a vertical line. The height is given by the brightness value from the camera. That’s how we plot the values. The other part in the code will be segmenting the graph in 7 parts and fading the colors from blue to red.





Spectrometer Code 13/11/2022

#include <TFT_HX8357.h>               //Download it here: https://electronoobs.com/eng_TFT_HX8357.php
TFT_HX8357 tft = TFT_HX8357();        // Create a new TFT display variable

#define CENTRE       240
#define clk          2                // clock pin
#define si           3                // start integration pin
#define pixel_value  A0               // pixel brightness pin
#define expo         A1               // potentiometer pin

int factor = 4;    
long exposure;
int pixels[128];
int i,j;

void setup()
{
  Serial.begin(38400);
  
  pinMode(si, OUTPUT);
  pinMode(si, OUTPUT);
  
  // Setup the LCD
  tft.init();
  tft.setRotation(1);
  
  tft.fillScreen(0x0000);
  tft.fillRect(0, 0, 480, 14, tft.color565(210, 210, 210));  

  tft.setTextColor(tft.color565(255, 255, 255),tft.color565(0, 0, 0));
  tft.drawCentreString("400", 10 + 19, 310, 1);
  tft.drawCentreString("440", 10 + 19 + 1 * 50.6, 310, 1);
  tft.drawCentreString("480", 10 + 19 + 2 * 50.6, 310, 1);
  tft.drawCentreString("520", 10 + 19 + 3 * 50.6, 310, 1);
  tft.drawCentreString("560", 10 + 19 + 4 * 50.6, 310, 1);
  tft.drawCentreString("600", 10 + 19 + 5 * 50.6, 310, 1);
  tft.drawCentreString("640", 10 + 19 + 6 * 50.6, 310, 1);
  tft.drawCentreString("680", 10 + 19 + 7 * 50.6, 310, 1);
  tft.drawCentreString("[nm]", 420, 310, 1);
  tft.drawCentreString("exposure =", 438, 140, 1);
  tft.drawCentreString("msec", 438, 180, 1);  
  
 }

void loop()
   {
    tft.drawLine(10, 300, 10 + 380, 300, tft.color565(255, 255, 255));   
    tft.drawLine(10 + 19 + 0 * 50.6, 300, 10 + 19 + 0 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 0.5 * 50.6, 300, 10 + 19 + 0.5 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 1.0 * 50.6, 300, 10 + 19 + 1.0 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 1.5 * 50.6, 300, 10 + 19 + 1.5 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 2.0 * 50.6, 300, 10 + 19 + 2.0 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 2.5 * 50.6, 300, 10 + 19 + 2.5 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 3.0 * 50.6, 300, 10 + 19 + 3.0 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 3.5 * 50.6, 300, 10 + 19 + 3.5 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 4.0 * 50.6, 300, 10 + 19 + 4.0 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 4.5 * 50.6, 300, 10 + 19 + 4.5 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 5.0 * 50.6, 300, 10 + 19 + 5.0 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 5.5 * 50.6, 300, 10 + 19 + 5.5 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 6.0 * 50.6, 300, 10 + 19 + 6.0 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 6.5 * 50.6, 300, 10 + 19 + 6.5 * 50.6, 305, tft.color565(255, 255, 255));
    tft.drawLine(10 + 19 + 7.0 * 50.6, 300, 10 + 19 + 7.0 * 50.6, 305, tft.color565(255, 255, 255));

    tft.fillRect(10,15,382,285,0xFF0F);

    exposure = analogRead(expo);        // read integration time from potentiometer.
    exposure = 200;              // Integrations-Intervall [0,255] ms

    Serial.print("Exposure = ");
    Serial.println(exposure);
    
    getCamera();

     
    tft.fillRect(410,160,70,10,TFT_WHITE);
    tft.setTextColor(tft.color565(255, 255, 255),tft.color565(0, 0, 0));
    tft.drawNumber(exposure, 428, 160,1);
    
   
    for(i = 0; i < 128; i++)
       {
        if(i >= 0 && i < 32)
           {       
            tft.drawLine(10 + 3*i, 299, 10 + 3*i, 299 - pixels[i], tft.color565(255 - int((255.0/32.0) * (float(i) + 1.0/3.0)), 0, 255));        
            tft.drawLine(10 + 3*i + 1, 299, 10 + 3*i + 1, 299 - (pixels[i] + (1.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(255 - int((255.0/32.0) * (float(i) + 2.0/3.0)), 0, 255));
            tft.drawLine(10 + 3*i + 2, 299, 10 + 3*i + 2, 299 - (pixels[i] + (2.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(255 - int((255.0/32.0) * i), 0, 255));           
           }

        if(i >= 32 && i < 48)
           {       
            tft.drawLine(10 + 3*i, 299, 10 + 3*i, 299 - pixels[i], tft.color565(0, int((255.0/16.0) * (i - 32)),255));        
            tft.drawLine(10 + 3*i + 1, 299, 10 + 3*i + 1, 299 - (pixels[i] + (1.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(0, int((255.0/16.0) * (i - 32 + 1.0/3.0)), 255));
            tft.drawLine(10 + 3*i + 2, 299, 10 + 3*i + 2, 299 - (pixels[i] + (2.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(0, int((255.0/16.0) * (i - 32 + 2.0/3.0)), 255));            
           }

        if(i >= 48 && i < 61)
           {       
            tft.drawLine(10 + 3*i, 299, 10 + 3*i, 299 - pixels[i], tft.color565(0, 255, 255 - int((255.0/13.0) * (i - 48))));        
            tft.drawLine(10 + 3*i + 1, 299, 10 + 3*i + 1, 299 - (pixels[i] + (1.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(0, 255, 255 - int((255.0/13.0) * (i - 48 + 1.0/3.0))));
            tft.drawLine(10 + 3*i + 2, 299, 10 + 3*i + 2, 299 - (pixels[i] + (2.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(0, 255, 255 - int((255.0/13.0) * (i - 48 + 2.0/3.0))));
           }

         if(i >= 61 && i < 82)
           {       
            tft.drawLine(10 + 3*i, 299, 10 + 3*i, 299 - pixels[i], tft.color565(0 + int((255.0/21.0) * (i - 61)), 255, 0));        
            tft.drawLine(10 + 3*i + 1, 299, 10 + 3*i + 1, 299 - (pixels[i] + (1.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(0 + int((255.0/21.0) * (i - 61 + 1.0/3.0)), 255, 0));
            tft.drawLine(10 + 3*i + 2, 299, 10 + 3*i + 2, 299 - (pixels[i] + (2.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(0 + int((255.0/21.0) * (i - 61 + 2.0/3.0)), 255, 0));
           }  
        
        if(i >= 82 && i < 107)
           {       
            tft.drawLine(10 + 3*i, 299, 10 + 3*i, 299 - pixels[i], tft.color565(255, 255 - int((255.0/25.0) * (i - 82)), 0));        
            tft.drawLine(10 + 3*i + 1, 299, 10 + 3*i + 1, 299 - (pixels[i] + (1.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(255, 255 - int((255.0/25.0) * (i - 82 + 1.0/3.0)), 0));
            tft.drawLine(10 + 3*i + 2, 299, 10 + 3*i + 2, 299 - (pixels[i] + (2.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(255, 255 - int((255.0/25.0) * (i - 82 + 2.0/3.0)), 0));           
            } 
        
        if(i >= 107 && i < 127)
           {       
            tft.drawLine(10 + 3*i, 299, 10 + 3*i, 299 - pixels[i], tft.color565(255 - int((255.0/34.0) * (i - 107)), 0, 0));        
            tft.drawLine(10 + 3*i + 1, 299, 10 + 3*i + 1, 299 - (pixels[i] + (1.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(255 - int((255.0/34.0) * (i - 107 + 1.0/3.0)), 0, 0));
            tft.drawLine(10 + 3*i + 2, 299, 10 + 3*i + 2, 299 - (pixels[i] + (2.0/3.0) * (pixels[i+1] - pixels[i])), tft.color565(255 - int((255.0/34.0) * (i - 107 + 2.0/3.0)), 0, 0));
           }

        if(i == 127)
           {
            tft.drawLine(10 + 3*i, 299, 10 + 3*i, 299 - pixels[i], tft.color565(0 + int((255.0/34.0) * (i - 107)), 255, 255));
           }
       }
     
    tft.setTextColor(tft.color565(20, 20, 255),tft.color565(210, 210, 210));
    tft.drawCentreString("Spectroscope", CENTRE, 3, 1);          
    delay(60);
       
   }




void getCamera()
   {
    digitalWrite(clk, LOW);
    digitalWrite(si, HIGH);
    digitalWrite(clk, HIGH);
    digitalWrite(si, LOW);
    digitalWrite(clk, LOW);
 
    for (int j = 0; j < 128; j++)
       {
        digitalWrite(clk, HIGH);
        digitalWrite(clk, LOW);
       }
 
    delayMicroseconds(exposure);
  
 
    digitalWrite(si, HIGH);
    digitalWrite(clk, HIGH);
    digitalWrite(si, LOW);
    digitalWrite(clk, LOW);
    
    for (int j = 0; j < 128; j++)   
       {
        delayMicroseconds(20);
        
        pixels[j] = analogRead(pixel_value) / factor;    // Brightness range [0,255]
        
        digitalWrite(clk, HIGH);
        digitalWrite(clk, LOW);
       }
 
    delayMicroseconds(20);
   }







Last tutorials

All about Arduino PWM frequencies
Tesla Coil on PCB
RLC Transistor Tester PCB with Arduino
Ferrofluid 3D printed Bluetooth Speaker
10A Bharger/Protection Module

ADVERTISERS



>

Affiliate Disclosure

ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel bajo