Reader small image

You're reading from  DIY Microcontroller Projects for Hobbyists

Product typeBook
Published inJul 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781800564138
Edition1st Edition
Languages
Right arrow
Authors (2):
Miguel Angel Garcia-Ruiz
Miguel Angel Garcia-Ruiz
author image
Miguel Angel Garcia-Ruiz

Miguel Angel Garcia-Ruiz is an Associate Professor of Computer Science at the School of Computer Science and Technology, Algoma University, Canada. He has taught microcontroller programming and interfacing, human-computer interaction, and interaction design courses. Miguel has a PhD in Computer Science and Artificial Intelligence from Sussex University, England. He has published articles on tinkering with technology applying microcontroller boards. Miguel has conducted research projects funded by Canada's Northern Ontario Heritage Fund (NOHFC), Algoma University, and the Mexican Ministry of Education.
Read more about Miguel Angel Garcia-Ruiz

Pedro Cesar Santana Mancilla
Pedro Cesar Santana Mancilla
author image
Pedro Cesar Santana Mancilla

Pedro Cesar Santana Mancilla is a research professor at the School of Telematics at the University of Colima in Mexico. His research interests focus on human-computer interaction, ICT for elderly people, Internet of Things, and machine learning. He is currently serving as president of the Mexican Association on Human-Computer Interaction (AMexIHC). He is a Senior Member of the IEEE, and ACM and serves as Chair of the Mexican ACM SIGCHI Chapter (CHI-Mexico). Pedro is a member of the Mexican Academy of Computing (AMexComp) and the Mexican Society of Computer Science (SMCC).
Read more about Pedro Cesar Santana Mancilla

View More author details
Right arrow

Chapter 11: IoT Solar Energy (Voltage) Measurement

Solar energy is considered one of the most promising renewable energy sources in the face of global warming challenges. It has been considered one of the best alternatives to reduce the dependency on fossil fuels and meet the growing demand for electricity (Ryan, 2005). To achieve this, sunlight is converted into electricity, and the sunlight is collected through solar panels.

In this chapter, you will continue creating IoT software for the STM32 Blue Pill microcontroller board using a voltage sensor to measure the solar energy collected by a solar panel. The application will send the sensed data to the internet using the NodeMCU ESP8266 microcontroller board.

In this chapter, we will cover the following main topics:

  • Connecting a solar panel to the Blue Pill board
  • Reading data from a voltage sensor module
  • Coding a program to send the sensed data to the internet
  • Showing sensor data results over the internet...

Technical requirements

The hardware components that will be needed to develop the solar energy measurement system are as follows:

  • One solderless breadboard.
  • One Blue Pill microcontroller board.
  • One NodeMCU microcontroller.
  • One ST-Link/V2 electronic interface for uploading the compiled code to the Blue Pill board. Bear in mind that the ST-Link/V2 requires four female-to-female jumper wires.
  • One B25 voltage sensor.
  • One solar panel.
  • Male-to-male jumper wires.
  • Female-to-male jumper wires.
  • Power source.

All the components can easily be found at your preferred electronics supplier. Remember, you will require the Arduino IDE and the GitHub repository for this chapter: https://github.com/PacktPublishing/DIY-Microcontroller-Projects-for-Hobbyists/tree/master/Chapter11

The Code in Action video for this chapter can be found here: https://bit.ly/2U4YMsT

The next section presents an introduction to the solar panels and the B25 voltage measurement...

Connecting a solar panel to the Blue Pill board

Firstly, we need to learn about two components: the solar panel and the voltage measurement sensor. After learning the basics, we can build our solar energy measurement system.

Introducing the solar panel

Sunlight carries energy. When sunlight collides with a semiconductor, some energy is changed into moving electrons, generating current. Solar cells (also known as photovoltaic panels or PV panels) were created to take advantage of all the sunlight that reaches our planet. When sunlight reflects off a PV panel, the current output is constant; this is known as direct current (DC) electricity. This DC can be used to charge batteries and power microcontrollers such as the STM32 Blue Pill.

The following screenshot shows a solar panel for use with electronic components, such as our solar energy demonstration system:

Figure 11.1 – Solar panel

Figure 11.1 – Solar panel

To facilitate the connection and operation with this...

Reading data from a voltage sensor module

It is time to learn how to code a program that will read the information from the voltage sensor and display its reading on the serial monitor.

Let's write the program to receive the sensor data from the STM32 Blue Pill:

  1. Declare which pin of the STM32 Blue Pill card will be used as input of the sensor data:
    const int sensorPin = 0;

    The input pin will be the 0 (labeled A0 on the Blue Pill).

  2. Next, in the setup() part, start the serial data transmission and assign the speed of the transfer to 9600 bps, and indicate to the microcontroller the type of pin assigned to A0:
    void setup() {
      Serial.begin(9600);
      pinMode(sensorPin, INPUT);
    }
  3. Now, in loop(), first read the input pin's data sensor, send its value to the serial port, and wait for a second:
    void loop() {
      int sensorValue = analogRead(sensorPin);
      Serial.print("Voltage: ");
      Serial.println(sensorValue...

Coding a program to send the sensed data to the internet

In this section, we will continue using the NodeMCU development board to receive the data from the STM32 and send it to the internet. However, unlike Chapter 10, IoT Plant Pot Moisture Sensor, where a digital value (1 or 0) was sent directly between both microcontrollers, we now need to send the voltage value using serial communication between these microcontrollers.

Serial transmission is done by sending the data using the RX/TX pins.

Let's create the program to connect the NodeMCU and the STM32:

  1. In setup(), we need to add new serial data transmission to 115200 bps. It is the recommended speed for the NodeMCU board:
    void setup() {
      serial.begin(9600);
      Serial1.begin(115200);
    }
  2. The loop() instance needs a new line after the sensor reading and voltage conversion. The write() function sends the data as an integer value:
    void loop() {
      int sensorValue = analogRead(sensorPin)...

Showing sensor data results over the internet

In Chapter 9, IoT Temperature-Logging System, and Chapter 10, IoT Plant Pot Moisture Sensor, we learned how to program IoT applications within our local network. In this section of the chapter, we will learn how to send data to the cloud outside of our local network.

A wide variety of cloud platforms allow us to connect our IoT devices to their services. Most allow us to use essential services at no cost. If something more complete is desired, there is a charge, generally a monthly payment. This time we will use the Blynk platform, which has several free options, and they are the ones we will use.

Blynk has an app for both Android and iOS that will allow us to monitor the value of the voltage in our solar cell.

Let's look at the steps to send and view our information from the internet with a mobile app:

  1. Download the Blynk app.

    For Android, download it from https://play.google.com/store/apps/details?id=cc.blynk&hl...

Summary

In this chapter dedicated to IoT, we have learned some essential topics. First, we got to know the solar cells used to power small electronic devices. Next, we learned about the B25 voltage sensor and how to connect it to the STM32.

Later, we learned how to create a program to read data from the voltage sensor. With the voltage reading, we connect our STM32 to a NodeMCU board through serial communication. We create a program to send the voltage value between microcontrollers. Finally, we use an app to visualize the sensor data from the cloud.

At the end of the IoT topics, you have solid skills to create applications and devices connected to the internet and intranets. Your portfolio of projects has been strengthened to enable you to more easily find a job opportunity in this growth area.

In the next chapter, you will start developing projects that will help you create electronic support devices to assist with the COVID-19 pandemic.

Further reading

Ryan, V., What Is Solar Energy? Technology Student, 2005: https://technologystudent.com/energy1/solar1.htm

lock icon
The rest of the chapter is locked
You have been reading a chapter from
DIY Microcontroller Projects for Hobbyists
Published in: Jul 2021Publisher: PacktISBN-13: 9781800564138
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Miguel Angel Garcia-Ruiz

Miguel Angel Garcia-Ruiz is an Associate Professor of Computer Science at the School of Computer Science and Technology, Algoma University, Canada. He has taught microcontroller programming and interfacing, human-computer interaction, and interaction design courses. Miguel has a PhD in Computer Science and Artificial Intelligence from Sussex University, England. He has published articles on tinkering with technology applying microcontroller boards. Miguel has conducted research projects funded by Canada's Northern Ontario Heritage Fund (NOHFC), Algoma University, and the Mexican Ministry of Education.
Read more about Miguel Angel Garcia-Ruiz

author image
Pedro Cesar Santana Mancilla

Pedro Cesar Santana Mancilla is a research professor at the School of Telematics at the University of Colima in Mexico. His research interests focus on human-computer interaction, ICT for elderly people, Internet of Things, and machine learning. He is currently serving as president of the Mexican Association on Human-Computer Interaction (AMexIHC). He is a Senior Member of the IEEE, and ACM and serves as Chair of the Mexican ACM SIGCHI Chapter (CHI-Mexico). Pedro is a member of the Mexican Academy of Computing (AMexComp) and the Mexican Society of Computer Science (SMCC).
Read more about Pedro Cesar Santana Mancilla