STM8S Temperature Monitor: LM35 & 7-Segment DIY Project

Table of Contents

  1. Introduction
  2. Circuit Diagram
  3. Setup Steps
    1. Install Arduino
    2. Install STM8S board manager and Flash the code
    3. Code
  4. Video Tutorial

1. Introduction : 

Learn how to build a temperature monitoring system using the STM8S microcontroller, LM35 temperature sensor, and a 7-segment display. We'll walk you through each step, from assembling the hardware to writing code in STM8S, ensuring you gain hands-on experience in creating your own electronic masterpiece. Subscribe now for a journey into the world of DIY electronics!

2. Circuit Diagram :

3. Setup Steps : 

3.1 Install Arduino

    1. Visit the Arduino Website:
      Go to the official Arduino website at https://www.arduino.cc/.
    2. Navigate to the Software Page:
      Hover over the "Software" tab in the top menu, and click on "Downloads."
    3. Download Arduino IDE:
      Click on the "Windows Installer" link to download the Arduino IDE for Windows.

3.2  Install STM8S board manager and Flash the code

1. Start the Arduino-IDE. In File->Preferences, Settings tab, enter

https://github.com/akashkapashia/stm8_arduino/raw/master/package_sduino_stm8_index.json

as an Additional Boards Manager URL.

2. Open Tools->Board:...->Boards Manager

      • Find Sduino by typing 'sd' into the search line
      • Click on the list entry
      • Click on Install.
      • Now you should find a new entry STM8S Boards in the list at Tools->Board:...

3. Choose STM8S103F3 Breakout Board from the list
Open : File >> Open >> Select the Attached .ino file
compile it by hitting Verify

3.3 Code

#define A_PIN PC5
#define B_PIN PD2
#define C_PIN PD3
#define D_PIN PD1
#define E_PIN PC6
#define F_PIN PC4
#define G_PIN PC7

#define S1_PIN PB4
#define S2_PIN PA3
#define S3_PIN PB5

#define LM35_INPUT_PIN PD6

// Define the digits for 0-9
const int digits[11][7] = {
  {1, 1, 1, 1, 1, 1, 0}, // 0
  {0, 1, 1, 0, 0, 0, 0}, // 1
  {1, 1, 0, 1, 1, 0, 1}, // 2
  {1, 1, 1, 1, 0, 0, 1}, // 3
  {0, 1, 1, 0, 0, 1, 1}, // 4
  {1, 0, 1, 1, 0, 1, 1}, // 5
  {1, 0, 1, 1, 1, 1, 1}, // 6
  {1, 1, 1, 0, 0, 0, 0}, // 7
  {1, 1, 1, 1, 1, 1, 1}, // 8
  {1, 1, 1, 1, 0, 1, 1}, // 9
  {1, 0, 0, 1, 1, 1, 0}  // c (C for Degree c)
};

float lm35_voltage = 0;
float average_temp = 0;
float temperature_degree_c = 0;

int raw_adc_reading = 0;
// 5000 = 5V
int ref_voltage = 5000;
// 10 bit ADC 1 << 10 = 2 ^ 10 = 1024
int adc_bit_size = 1 << 10;
int average_temp_sample_size = 10;

// Function to display a digit on a specific display
void displayDigit(int digit, int display_pin) {
  int num = 0, nums = 0;
  int digits_pins[7] = {A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, F_PIN, G_PIN};
  
  for(num = 0; num < 7; num++)
  {
    digitalWrite(digits_pins[num], 0);
  }

  for(num = 0; num < 7; num++)
  {
    digitalWrite(digits_pins[num], digits[digit][num]);
  }
  // Select the display
  digitalWrite(S1_PIN, display_pin == 1 ?  LOW : HIGH);
  digitalWrite(S2_PIN, display_pin == 2 ?  LOW : HIGH);
  digitalWrite(S3_PIN, display_pin == 3 ?  LOW : HIGH);
}

// Function to display a 2-digit number on the 7-segment display
void displayNumberWithDegreeC(int number) {
  int digit1 = number % 10;
  int digit2 = (number / 10) % 10;
  // Display the digits on each display, one at a time
 
  displayDigit(digit1, 1);
  delay(5); // Adjust delay as needed
  displayDigit(digit2, 2);
  delay(5); // Adjust delay as needed
  displayDigit(10, 3);
  delay(1); // Adjust delay as needed
}

void setup() {
  // Set pin modes
  pinMode(A_PIN, OUTPUT);
  pinMode(B_PIN, OUTPUT);
  pinMode(C_PIN, OUTPUT);
  pinMode(D_PIN, OUTPUT);
  pinMode(E_PIN, OUTPUT);
  pinMode(F_PIN, OUTPUT);
  pinMode(G_PIN, OUTPUT);

  pinMode(S1_PIN, OUTPUT);
  pinMode(S2_PIN, OUTPUT);
  pinMode(S3_PIN, OUTPUT);

  //Disabling the PNP Transistor pin to turn on base by default
  digitalWrite(S1_PIN, 1);
  digitalWrite(S2_PIN, 1);
  digitalWrite(S3_PIN, 1);

  //Disabling the NPN Transistor pin to turn off base by default
  digitalWrite(A_PIN, 0);
  digitalWrite(B_PIN, 0);
  digitalWrite(C_PIN, 0);
  digitalWrite(D_PIN, 0);
  digitalWrite(E_PIN, 0);
  digitalWrite(F_PIN, 0);
  digitalWrite(G_PIN, 0);
}

void loop()
{
  for(int avg = 0; avg < average_temp_sample_size; avg++) 
  {
    raw_adc_reading = analogRead(LM35_INPUT_PIN);
    // Convert the reading into voltage:
    // Convert digital data into analog by multiplying by 5000 and dividing by 1024
    lm35_voltage = raw_adc_reading * (ref_voltage / adc_bit_size);
    // Convert the voltage into the temperature in degree Celsius:
    float temperature_degree_c = lm35_voltage / 10;
    average_temp = average_temp + temperature_degree_c; 
  }
  average_temp = average_temp / average_temp_sample_size;
  // Example: Display the number 123
  displayNumberWithDegreeC(average_temp);
}

3. Video Step by Step Tutorial :