Hello,
I have a problem using a fotoresistor that detects brightness connected to the Arduino Nano rp2040 Connect.
this is the fotoresistor that I'm using: Fotoresistor
This is my Arduino: Arduino
Until yesterday I connected it to our circuit with two female to female cables and it worked perfectly, now it doesn't work anymore.
So we don't understand if the problem is the welds that are starting to become unstable or some cables that no longer conduct.
The fotoresistor is connected, on one side, to the 3.3v, and on the other side to A3 Pin and GND. There is obviously a 1k OHM resistor.
what do you mean?
It used to detect the light correctly either by pointing on a strong light and by putting a finger on it so the light decreased till the value of 4-5.
the wires are around 35 cm long. I don't think it's a length problem because it worked with those 35cm + the female to female cables as in the first picture that I've sent before.
Regarding the welding quality I am an amateur so I did my best...
Do you think that welding the sensor cables again on the Arduino side could be a possibile solution?
Also I tried to upload the same code with the LDR connected through the breadboard (and not soldered) and it also worked.
The only think that remains a possible cause of the problem is the soldering work.
Also sorry for the misunderstandment ("welding" instead of "soldering") but I thought it was the right translation from Italian
So I've checked the soldering and, by changing the Analog Pin it started working.
The fact is that the sensor values should be sent to another Arduino through BLE Communication. logic also used to work yesterday but now when I upload the code of both the two arduinos the values of the LDR sensor are always: 0, even though the LDR works (tested through other codes.)
This is the code of the Arduino Nano RP2040 Connect that has the LDR connected (PERIPHERAL):
#include <WiFiNINA.h>
#include <ArduinoBLE.h>
#define LIGHT_SENSOR_PIN A6 //pin del sensore di luce
#define LIGHT_SENSOR_CALIBRATION_SAMPLES 10 //numero di campioni per la calibrazione del sensore di luce
#define LIGHT_SENSOR_CALIBRATION_DELAY 50 //ms il ritardo tra i campioni
#define LIGHT_SENSOR_THRESHOLD 70//% w.r.t. max ambient value
#define PRINT_CONDITION_MIN_TIME 5000 //ms. non penso serva?????
#define PRESSURE_SENSOR_THRESHOLD 8
int fsrPin = 0;
int fsrPin2 = 1;
int fsrPin3 = 2;
unsigned short light;
unsigned short light_threshold = 0;
BLEService FSRService("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEUnsignedLongCharacteristic FSRCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
BLEUnsignedLongCharacteristic LightCharacteristic("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
void setup() {
Serial.begin(9600);
while(!Serial);
pinMode(fsrPin, INPUT);
pinMode(fsrPin2, INPUT);
pinMode(fsrPin3, INPUT);
Serial.print("Performing light sensor calibration, ETA ");
Serial.print(LIGHT_SENSOR_CALIBRATION_SAMPLES * LIGHT_SENSOR_CALIBRATION_DELAY);
Serial.println(" ms");
int measurement_counter = 0;
int max_light = 0;
while(measurement_counter < LIGHT_SENSOR_CALIBRATION_SAMPLES){
light = analogRead(LIGHT_SENSOR_PIN);
if(light > max_light){
max_light = light;
}
measurement_counter++;
delay(LIGHT_SENSOR_CALIBRATION_DELAY);
}
light_threshold = max_light * LIGHT_SENSOR_THRESHOLD / 100;
Serial.print("Light threshold set to ");
Serial.println(light_threshold);
if (!BLE.begin()) {
Serial.println("Starting Bluetooth®️ Low Energy failed!");
while (1);
}
BLE.setLocalName("FSR Sensor");
BLE.setAdvertisedService(FSRService);
FSRService.addCharacteristic(FSRCharacteristic); // Aggiungi la nuova caratteristica per il sensore di forza
FSRService.addCharacteristic(LightCharacteristic); // Aggiungi la nuova caratteristica per la luce
BLE.addService(FSRService);
BLE.advertise();
Serial.println("BLE FSR Sensor Peripheral, waiting for connections...");
}
unsigned long start_time = 0;
bool is_triggered = false;
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connesso al nano centrale: ");
Serial.println(central.address());
while (central.connected()) {
unsigned short sensorState = analogRead(fsrPin);
Serial.print("Force sensor 1 reading = ");
Serial.print(sensorState);
unsigned short sensorState2 = analogRead(fsrPin2);
Serial.print(", Force sensor 2 reading = ");
Serial.print(sensorState2);
unsigned short sensorState3 = analogRead(fsrPin3);
Serial.print(", Force sensor 3 reading = ");
Serial.print(sensorState3);
unsigned short maxReading = max(sensorState, sensorState2);
//Serial.print(", Maxreading = ");
//Serial.println(maxReading);
light = analogRead(LIGHT_SENSOR_PIN);
if (light > light_threshold) {
digitalWrite(13, HIGH); //accendi e sepgni il led integrato nell'arduino
//Serial.println("Light reading = " + String(light) + "(high)");
} else {
digitalWrite(13, LOW);
//Serial.println("Light reading = " + String(light) + "(low)");
}
/*Serial.println("Sensore 1:");
Serial.println(sensorState);
Serial.println("");
Serial.println("Sensore 2:");
Serial.println(sensorState2);*/
Serial.println("Sending Light: " + String(light) + " Pressure: " + String(maxReading) + " Light threshold: " + String(light_threshold));
LightCharacteristic.writeValue( ( light & 0b11111111111 | (( maxReading & 0b11111111111) << 11)) | ((light_threshold & 0b1111111111) << 22) );
delay(500);
//FSRCharacteristic.writeValue(maxReading);
//LightCharacteristic.writeValue(light); // Aggiungi il valore di light alla caratteristica BLE
}
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
This is the code of the Arduino NanoRP2040Connect that should receive the values (CENTRAL):
#include <ArduinoBLE.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Adafruit_Thermal.h"
// Tempo utilizzo
int stato = 0;
unsigned long data;
unsigned short light;
unsigned short pressure;
unsigned short light_threshold;
// Tempo per schermino
unsigned long lcdstartMillis;
unsigned long lcdcurrentMillis;
//tempi per animazione
unsigned long animstartMillis;
unsigned long animcurrentMillis;
const unsigned long animperiod = 30000;
#define BLE_SCAN_DELAY 10 // Ogni quanto rinizia lo scan BLE
#define PERIPHERAL_UUID "19b10000-e8f2-537e-4f6c-d104768a1214"
#define FSR_UUID "19b10001-e8f2-537e-4f6c-d104768a1214"
#define LIGHT_UUID "19b10002-e8f2-537e-4f6c-d104768a1214"
#define PRESSURE_SENSOR_THRESHOLD 1 // Soglia da superare per attivazione
#define PRINT_CONDITION_MIN_TIME 5000 // ms
// Schermino
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST -1
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Colori schermino
#define TFT_BIANCO 0xFFFF
#define TFT_ROSA 0xFE59
#define TFT_ROSSO 0xD0A4
Adafruit_Thermal printer(&Serial1);
bool is_triggered = false;
unsigned long start_time = 0;
unsigned long elapsed_time;
void setup() {
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
Serial.begin(9600);
Serial1.begin(19200);
printer.begin();
lcdstartMillis = millis(); // Inizio del tempo di visualizzazione
while (!Serial1); // L'istruzione while (!Serial) attende finché la comunicazione seriale non è stabilita.
Serial.println("Connessione Serial1 stabilita");
if (Serial) {
Serial.println("Connessione seriale USB stabilita");
}
// Schermino
tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BIANCO);
// Bluetooth
BLE.begin();
Serial.println("BLE Central - Controllo FSR");
BLE.scanForUuid(PERIPHERAL_UUID);
tft.drawRGBBitmap(0, 0, (uint16_t *)logoBitmap, LOGO_WIDTH, LOGO_HEIGHT);
}
void loop() {
BLEDevice peripheral = BLE.available();
if (peripheral) {
Serial.print("Trova ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
if (peripheral.localName().indexOf("FSR Sensor") < 0) {
Serial.println("Nessun 'FSR Sensor' nel nome");
return;
}
BLE.stopScan();
loop2(peripheral);
BLE.scanForUuid(PERIPHERAL_UUID);
} else {
Serial.print("Periferico BLE non disponibile, riprova in ");
Serial.print(BLE_SCAN_DELAY);
Serial.println(" ms");
delay(BLE_SCAN_DELAY);
}
}
void loop2(BLEDevice peripheral) {
Serial.println("Connessione ...");
if (peripheral.connect()) {
Serial.println("Connesso");
} else {
Serial.println("Impossibile connettersi!");
return;
}
Serial.println("Scoperta attributi ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributi scoperti");
} else {
Serial.println("Scoperta attributi fallita!");
peripheral.disconnect();
return;
}
BLECharacteristic FSRCharacteristic = peripheral.characteristic(FSR_UUID);
if (!FSRCharacteristic) {
Serial.println("Il periferico non ha la caratteristica FSR!");
peripheral.disconnect();
return;
}
BLECharacteristic LightCharacteristic = peripheral.characteristic(LIGHT_UUID);
if (!LightCharacteristic) {
Serial.println("Il periferico non ha la caratteristica Light!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
if (LightCharacteristic.canRead()) {
unsigned long data = 0;
LightCharacteristic.readValue(data);
unsigned short light = (unsigned short)(data & 0b00000000000000000000011111111111);
unsigned short pressure = (unsigned short)((data & 0b00000000001111111111100000000000) >> 11);
unsigned short light_threshold = (unsigned short)((data & 0b11111111110000000000000000000000) >> 22);
Serial.println("Light: " + String(light) + " Pressure: " + String(pressure) + " Light threshold: " + String(light_threshold));
}
As mentioned the soldering leaves much to be desired. I suggest if you can find a scrap board or two practice your soldering. Good soldering is a skill developed with practice. Can we just refer to soldering as soldering and not welding?
Next I would start simple and I would eliminate the 1K resistor replacing it with a 10K resistor. Next I suggest you read this Arduino / LDR and make a circuit just like what is shown in the link. Start simple and understand what is happening. Once you have that very basic and simple circuit working and understand what is going on then move to more complex.