Hi there,
I am currently working on a project that will eventually read a mass from an RS232 output to a MAX3232 from a scale (US Solid electronic balance SLSC Series) to an Arduino UNO R3. Based on the mass that the scale outputs, a humidifier and/or a fan/heater will either turn on (or off) using an IoT relay (ac/dc control relay) and will stop when the DHT22 reads a certain temperature or humidity. I have successfully gotten the DHT22 to produce results (both temperature and humidity) on a 20x4 LCD. I have also gotten a fan (dummy test) to turn on based on the humidity controls. This part I have a pretty good understanding how to control with my code. However, I am struggling to produce readable results from the balance (the rs232). I have tried many different things but the reproduced data is showing up as zero whereas the raw data shows as the correct mass. However, the mass will not show up on the LCD with the humidity and temperature results. I am (sort of) new to Arduino but have researched a lot for this project but I just cannot seem to get it to print correctly. Below I have attached photo schematics, the user manual to the scale, and my rough code. Not shown are the humidifier, fan, or the heater. The main concern is being able to read the mass from the scale in a readable format on the LCD. Half of the time the mass shows on the serial monitor correctly, but other times it will just show as zero.
Side note: So far it looks very ugly, but once I figure out how to make it fully work, I will set up the heater and other accessories nicer.
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
#define LCD_ADDRESS 0x27
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int controlPin = 3; // Adjust this to the pin connected to the "IN" on the IoT Relay
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
dht.begin();
lcd.init();
lcd.begin(20, 4); // Adjust the LCD size to 20 columns and 4 rows
lcd.backlight();
pinMode(LED_BUILTIN, OUTPUT);
pinMode(controlPin, OUTPUT);
digitalWrite(controlPin, LOW); // Ensure the relay is initially in the off state
}
void loop() {
// Read and display mass from the scale
while (mySerial.available()) {
String receivedData = mySerial.readStringUntil('\n'); // Read until newline character
if (receivedData.length() > 0) {
Serial.print("Raw Data: ");
Serial.println(receivedData); // Debugging print
// Extract numerical part and parse as float
String numericalPart = receivedData.substring(1, receivedData.length() - 1); // Remove the '+' and 'g'
float mass = numericalPart.toFloat();
Serial.print("Parsed Mass: ");
Serial.println(mass); // Debugging print
// Read temperature and humidity from DHT sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mass: ");
lcd.print(mass);
lcd.print(" g");
lcd.setCursor(0, 1);
lcd.print("Temperature: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 2);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
// Control the IoT Relay based on the mass (example: turn on for mass > 100)
if (mass > 100) {
digitalWrite(controlPin, HIGH); // Turn on the relay
} else {
digitalWrite(controlPin, LOW); // Turn off the relay
}
// For debugging, print to serial monitor
Serial.print("Received mass: ");
Serial.println(mass);
}
}
// Other tasks can be performed here without a long delay
delay(5000);
}
I have never used this forum besides reading them so please bear with me.