I am trying to get the temp from a DS18b20 module to show on a 3 digit 7 segment display (common anode). I have successfully connected the display and ran the SevSeg_Counter code from the SevSeg examples. I have also got the temp module connected and successfully ran code to get the temp to display in the Serial monitor. I cant figure out how to pass the temp data to the 7 seg display.
Uno R3
using these libraries:
onewire.h
DallasTemperature.h
SevSeg.h
I have googled, nothing with same setup. (libraries, hardware). here are the two sketches.
I have attempted to combine the code but it isnt clear how the display works to me.
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object
void setup() {
byte numDigits = 3;
byte digitPins[] = {8, 9, 10};
byte segmentPins[] = {0, 1, 2, 3, 4, 5, 6, 7};
bool resistorsOnSegments = true; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void loop() {
static unsigned long timer = millis();
static int deciSeconds = 0;
if (millis() - timer >= 100) {
timer += 100;
deciSeconds++; // 100 milliSeconds is equal to 1 deciSecond
if (deciSeconds == 10000) { // Reset to 0 after counting for 1000 seconds.
deciSeconds=0;
}
sevseg.setNumber(deciSeconds, 1);
}
sevseg.refreshDisplay(); // Must run repeatedly
}
/// END ///
Temp code
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 11
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/*
* The setup function. We only start the sensors here
*/
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
}
/*
* Main function, get and show the temperature
*/
void loop(){
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
float temperatureF = sensors.getTempFByIndex(0);
Serial.print(temperatureC);
Serial.println("ºC");
Serial.print(temperatureF);
Serial.println("ºF");
delay(5000);
}
If you use pins 0 and 1 for the 7 segment display, you cannot use Serial. Pins 0 and 1 are the hardware serial port (Serial) pins. Serial prints are your most powerful debugging tool. I would suggest using it.
That's the usual result, what can be expected.
This business is different from playing with standard LEGO bricks. Learning programming and electronics makes You design things on Your own instead of copying other designs.
This is the line,where you control what to display.
The other sketch creates values in the variables temperatureC and temperatureF.
@groundFungus' hint about freeing pins 0 and 1 to keep Serial available for testing purposes should be observed.
Perhaps you start with the sevseg version and move pins 0 and 1, then add Serial test output.
Then experiment with output other than those sample deciSeconds.
Yes I have shifted the pins to free up 0 &1. and I have input my temperatureF variable as well as experiment with different numbers. I have also discovered that sevseg doesnt like "delay" so I have incorporated the millis() code from Blink without delay Yet the numbers just display one at a time from left to right. for example the temp. shown in the serial monitor is 74.97ºF, the 3 digit display flashes "0" in first digit then "7" in second digit then "4" in third, repeats.
not sure why it flashes a zero in first digit
cant get it to display the numbers at the same time
updated code
#include <OneWire.h>
#include <DallasTemperature.h>
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object
// GPIO where the DS18B20 is connected to
const int oneWireBus = 13;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
unsigned long previousMillis = 0;
const long interval = 5000;
void setup() {
// Start the Serial Monitor
Serial.begin(9600);
// Start the DS18B20 sensor
sensors.begin();
byte numDigits = 3;
byte digitPins[] = {10, 11, 12};
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
bool resistorsOnSegments = true; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void loop() {
sensors.requestTemperatures();
float temperatureF = sensors.getTempFByIndex(0);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
Serial.print(temperatureF);
Serial.println("ºF");
sevseg.setNumber(temperatureF,3);
previousMillis = millis();
}
sevseg.refreshDisplay();
}
well, changing the code to sevseg.setNumber((int)(temperatureF*10),1); got rid of the leading zero so that it now reads out the correct temp., but still one digit at a time.
An interesting side note, is that I have created the project on a Wokwi simulator and it appears to work correctly running in the simulator. just not in the real world.