Problem with Color Bar Graph for Visible Light Sensor

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
seedpress
 
Posts: 6
Joined: Sun Jan 22, 2023 3:01 am

Problem with Color Bar Graph for Visible Light Sensor

Post by seedpress »

I’m trying to run an Arduino sketch that reads the AS7262 6-Channel Visible Light Sensor and creates a color bar graph on the Adafruit 0.96" 160x80 Color TFT Display (as detailed on pages 10-12 of Dean Miller’s “Adafruit AS7262 6-channel Visible Light
Sensor pdf.”)

The sketch isn’t working!

My board is the Metro M4 Express. Content of my bootloader folder’s INFO_UF2.TXT:
UF2 Bootloader v3.15.0 SFHWRO
Model: Metro M4 Express
Board-ID: SAMD51J19A-Metro-v0

I’ve tested the AS7252 breakout and the TFT display using CircuitPython, and they are both functioning well, independently.

Prior to uploading the color bar graph sketch referenced above--as I'm new to using the Arduino IDE--I tested the Arduino IDE’s communication with the Metro board by running the blink sketch, as detailed on pages 42-43 of the pdf version of “Adafruit Metro M4 Express featuring ATSAMD51.” I could easily edit the c code to change the blink rate. So that works.

I have installed the Arduino AS7262 library and dependencies, as well as those for the display.

The color bar graph sketch verifies, compiles, and uploads, but when it runs I get nothing on the TFT display, and no error codes in Arduino’s IDE.

Not sure what to try next!!!

User avatar
mikeysklar
 
Posts: 13945
Joined: Mon Aug 01, 2016 8:10 pm

Re: Problem with Color Bar Graph for Visible Light Sensor

Post by mikeysklar »

Which version of the Arduino IDE are you using? Since the example code is five years old it would be worth trying the last 1.8.19 release of the Arduino IDE instead of the current 2.0.4.

https://www.arduino.cc/en/software

User avatar
seedpress
 
Posts: 6
Joined: Sun Jan 22, 2023 3:01 am

Re: Problem with Color Bar Graph for Visible Light Sensor

Post by seedpress »

I'm currently using 2.04. Thanks for the suggestion. It's worth a try.

User avatar
seedpress
 
Posts: 6
Joined: Sun Jan 22, 2023 3:01 am

Re: Problem with Color Bar Graph for Visible Light Sensor

Post by seedpress »

We got the bar graph working. It required a slight re-write of the code.

I'll post the code at the end.

The problem with the example in the tutorial was primarily that the Fritzing diagram did not match the code that followed it. In the Fritzing the Metro board is wired to the TFT by way of the assignable data pins rather than the dedicated hardware SPI pins, which are not reassignable. On the other hand, the code uses the hardwired SPI connector. We had wired up our board by following the Fritzing. So, we changed the code to match the Fritzing.

The other change we made was to use the current initialization code that became necessary because of the January 27, 2023 firmware update to the Tiny TFT display.

The code:

Code: Select all

#include <Wire.h>
#include "Adafruit_AS726x.h"

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>

#define TFT_CS     10
#define TFT_RST    9  // you can also connect this to the Arduino reset
                      // in which case, set this #define pin to -1!
#define TFT_DC     8

// added: pins for software SPI mode
#define TFT_MOSI 11  // Data out
#define TFT_SCLK 13  // Clock out

#define SENSOR_MAX 5000

#define BLACK   0x0000
#define GRAY    0x8410
#define WHITE   0xFFFF
#define RED     0xF800
#define ORANGE  0xFA60
#define YELLOW  0xFFE0  
#define LIME    0x07FF
#define GREEN   0x07E0
#define CYAN    0x07FF
#define AQUA    0x04FF
#define BLUE    0x001F
#define MAGENTA 0xF81F
#define PINK    0xF8FF

uint16_t colors[] = {
  MAGENTA,
  BLUE,
  GREEN,
  YELLOW,
  ORANGE,
  RED
};

// new: this constructs tft in "software" SPI mode
// For ST7735-based displays, we will use this call
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

//create the object
Adafruit_AS726x ams;

//buffer to hold raw values (these aren't used by default in this example)
//uint16_t sensorValues[AS726x_NUM_CHANNELS];

//buffer to hold calibrated values
float calibratedValues[AS726x_NUM_CHANNELS];

uint16_t barWidth;

void setup() {
  // originally 9600
  Serial.begin(9600);

  // old: init for rev A board
  //tft.initR(INITR_MINI160x80);   // initialize a ST7735S chip, mini display

  // new: init for rev B board
  tft.initR(INITR_MINI160x80_PLUGIN);  // Init ST7735S mini display
  
  tft.setRotation(3);

  tft.fillScreen(ST7735_BLACK);

  barWidth = tft.width() / AS726x_NUM_CHANNELS;
  
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  //begin and make sure we can talk to the sensor
  if(!ams.begin()){
    Serial.println("could not connect to sensor! Please check your wiring.");
    while(1);
  }
  
  ams.setConversionType(MODE_2);

  //uncomment this if you want to use the driver LED (off by default)
  //ams.drvOn();
}

void loop() {

  if(ams.dataReady()){
    
    //read the values!
    //ams.readRawValues(sensorValues);
    ams.readCalibratedValues(calibratedValues);
    
    for(int i=0; i<AS726x_NUM_CHANNELS; i++){
      uint16_t height = map(calibratedValues[i], 0, SENSOR_MAX, 0, tft.height());

      tft.fillRect(barWidth * i, 0, barWidth, tft.height() - height, ST7735_BLACK);
      tft.fillRect(barWidth * i, tft.height() - height, barWidth, height, colors[i]);
    }
  }
  
}

User avatar
mikeysklar
 
Posts: 13945
Joined: Mon Aug 01, 2016 8:10 pm

Re: Problem with Color Bar Graph for Visible Light Sensor

Post by mikeysklar »

Thank you for posting the full code. I assume you were also able to use the current 2.0.4 Arduino IDE?

User avatar
seedpress
 
Posts: 6
Joined: Sun Jan 22, 2023 3:01 am

Re: Problem with Color Bar Graph for Visible Light Sensor

Post by seedpress »

Yes, I used 2.04. I didn't get around to trying the older IDE.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”