Hello everyone,
I am working on a project where I am trying to read solar radiation data using a SEM228 total solar radiation sensor. I am using an Arduino Uno and a MAX485 module for RS485 communication. However, I am having trouble receiving data from the sensor. I have followed the datasheet instructions and tried multiple troubleshooting steps, but I still get "no response from the sensor" on the Serial Monitor. Here are the details:
Hardware :
- Microcontroller: Arduino Uno
- Sensor: SEM228 total solar radiation sensor
- RS485 Module: MAX485
- Power Supply: The solar radiation sensor is powered by 12v power supply and the MAX485 is powered directly from the 5V pin of the Arduino.
Code: Here is the code I am currently using:
#include <SoftwareSerial.h>
#define RE 8
#define DE 7
const byte pyranometer[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
byte values[8];
SoftwareSerial mod(2, 3);
void setup()
{
Serial.begin(9600);
mod.begin(4800);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
digitalWrite(RE, LOW);
digitalWrite(DE, LOW);
delay(1000);
}
unsigned int calculateCRC(byte *array, int length)
{
unsigned int crc = 0xFFFF;
for (int pos = 0; pos < length; pos++) {
crc ^= (unsigned int)array[pos];
for (int i = 8; i != 0; i--) {
if ((crc & 0x0001) != 0) {
crc >>= 1;
crc ^= 0xA001;
}
else
crc >>= 1;
}
}
return crc;
}
void loop()
{
// Transmit the request to the sensor
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(pyranometer, sizeof(pyranometer));
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
delay(10); // Allow time for the sensor to respond
unsigned long startTime = millis();
byte index = 0;
while (mod.available() < 7 && millis() - startTime < 1000)
{
delay(1);
}
// Read sensor response
if (mod.available() >= 7) {
while (mod.available() && index < 8)
{
values[index] = mod.read();
Serial.print(values[index], HEX);
Serial.print(" ");
index++;
}
Serial.println();
// Verification CRC
unsigned int receivedCRC = (values[index - 1] << 8) | values[index - 2];
unsigned int calculatedCRC = calculateCRC(values, index - 2);
if (calculatedCRC == receivedCRC) {
int Solar_Radiation = int(values[3] << 8 | values[4]);
Serial.print("Solar Radiation: ");
Serial.print(Solar_Radiation);
Serial.println(" W/m^2");
} else {
Serial.println("Error CRC - invalid data");
}
} else {
Serial.println("No response from the sensor");
}
delay(3000);
}
Problem: Even after these attempts, I always get "no response from the sensor" on the Serial Monitor.
I would appreciate any advice or suggestions on what might be wrong with my setup or code. If anyone has experience with this sensor or RS485 communication, your help would be greatly appreciated!
Thank you in advance!