LedControl.h - guidance

I’m struggling to get a Temperature to display on a MAX7219 7 segment.

I’ve worked out how to get numbers onto the display, but sending the temperature I just can’t work out.

My code in included below, you’ll see at the end of the code where I’m displaying a “8” on the 7 segment but the "temp"is not displaying.

Any guidance on what I’m doing wrong would be appericated.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <AsyncMqttClient.h>
#include <LedControl.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include <Wire.h>

#define WIFI_SSID "WiFi-86F4"
#define WIFI_PASSWORD "75831431"

LedControl lc=LedControl(12,14,2,1);

// Raspberri Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, 1, 109)
// For a cloud MQTT broker, type the domain name
//#define MQTT_HOST "example.com"
#define MQTT_PORT 1883

// Temperature MQTT Topics
#define MQTT_PUB_TEMP "esp/ds18b20/temperature2"

// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;          
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);
// Temperature value
float temp;

Adafruit_7segment matrix = Adafruit_7segment();
//Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;

WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;

unsigned long previousMillis = 0;   // Stores last time temperature was published
const long interval = 10000;        // Interval at which to publish sensor readings

void connectToWifi() {
  Serial.println("Connecting to Wi-Fi...");

void onWifiConnect(const WiFiEventStationModeGotIP& event) {
  Serial.println("Connected to Wi-Fi.");

void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
  Serial.println("Disconnected from Wi-Fi.");
  mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
  wifiReconnectTimer.once(2, connectToWifi);

void connectToMqtt() {
  Serial.println("Connecting to MQTT...");

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  Serial.println("Disconnected from MQTT.");

  if (WiFi.isConnected()) {
    mqttReconnectTimer.once(2, connectToMqtt);

/*void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
  Serial.println("Subscribe acknowledged.");
  Serial.print("  packetId: ");
  Serial.print("  qos: ");

void onMqttUnsubscribe(uint16_t packetId) {
  Serial.println("Unsubscribe acknowledged.");
  Serial.print("  packetId: ");

void onMqttPublish(uint16_t packetId) {
  Serial.print("Publish acknowledged.");
  Serial.print("  packetId: ");

void setup() {

  lc.shutdown(0,false);   // Enable display
  lc.setIntensity(0,10);  // Set brightness level (0 is min, 15 is max)

 // alpha4.begin(0x70); 

  wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
  wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);

  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  // If your broker requires authentication (username and password), set them below
  //mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");

void loop() {
  unsigned long currentMillis = millis();
  // Every X number of seconds (interval = 10 seconds) 
  // it publishes a new MQTT message
  if (currentMillis - previousMillis >= interval) {
    // Save the last time a new reading was published
    previousMillis = currentMillis;
    // New temperature readings
    // Temperature in Celsius degrees
    temp = sensors.getTempCByIndex(0);
    // Temperature in Fahrenheit degrees
    //temp = sensors.getTempFByIndex(0);

  //  sensors.requestTemperatures(); //this works
    // Publish an MQTT message on topic esp/ds18b20/temperature
    uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());                            
    Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_TEMP, packetIdPub1);
    Serial.printf("Message: %.2f \n", temp);

   // alpha4.writeDigitRaw(3, 0xFFFF);
   // matrix.print(sensors.getTempCByIndex(0)); // this works

lc.setDigit(0,0,temp,false); // attempt to display the temp - does not work!
lc.setDigit(0,1,8,false); // does display the 8 - just a test piece of code to see if LED display is working



I admit that I know nothing about setDigit(...), but I can clearly see that the variable temp is declared as float and that 8 is a small, single digit int.

Unless there are two setDigit(...) functions, one that takes a float and one that takes an int, there are going to be problems.

This (in loop() ) will convert the floating point “tempC” to 2 ints, “itempC” (whole degrees) and “ftempC” (tenths of a degree), which you can send to your display, don’t forget the decimal point.

float tempC = 25.5625; // 25 and 9/16
void setup()

void loop()
  tempC += (tempC < 0.0) ? -0.05 : 0.05; // for rounding
  int dtempC = tempC * 10; // multiply by 10
                           // and round to nearest tenth
  int itempC = dtempC / 10; // integer part (whole) degrees
  int ftempC = abs(dtempC) % 10; // fractional part (tenths)
  while(1); // loop here, press reset to repeat

LAST EDIT (I hope :slight_smile: ) Dealing with negative numbers.

The OP has already forgotten his this post about MAX7219/LedControl.h and now he has come again with similar post to fool the Forum members?

if you want to simplify the printing of numbers and characters on the MAX7219, you can try my Noiasca Led Control Library and just set the cursor and print whatever you have...


just one decimal point? ok, the same like for the serial monitor:

lc.print(temp, 1);

Don't forget to check the examples.

Hello all,

I am working on creating a pong game using an 8x8 LED Matrix. I want to implement scrolling text, but want to do so using the LedControl library, as I was told using 2 libraries (such as using MD_Parola in conjunction with LedControl) to control the same hardware is bad practice. Any way I could create scrolling text using only the LedControl library?

You hijack and necro a thread from September, to ask this? Start your own threads.