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 4: Measuring the Amount of Light with a Photoresistor

This chapter focuses on how to connect a photoresistor, an electronic component that measures the amount of light from the environment, to an input port of both the Blue Pill and Curiosity Nano microcontroller boards. In this chapter's exercise, we will analyze with a photoresistor whether a plant receives enough light.

In this chapter, we are going to cover the following main topics:

  • Understanding sensors
  • Introducing photoresistors
  • Connecting a photoresistor to a microcontroller board port
  • Coding the photoresistor values and setting up ports
  • Testing the photoresistor

By the end of this chapter, you will have learned how to connect an analog sensor to a microcontroller board, and how to analyze analog data obtained from a photoresistor. The knowledge and experience learned in this chapter will be useful in other chapters that require the use of a sensor.

Technical requirements

The software tools that you will be using in this chapter are the Arduino IDE and the MPLAB-X for editing and uploading your programs to the Blue Pill and the Curiosity Nano boards, respectively.

The code used in this chapter can be found in the book's GitHub repository here:

https://github.com/PacktPublishing/DIY-Microcontroller-Projects-for-Hobbyists/tree/master/Chapter04

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

In this chapter, we will also use the following pieces of hardware:

  • A solderless breadboard.
  • The Blue Pill and Curiosity Nano microcontroller boards.
  • A micro-USB cable for connecting your microcontroller boards to a computer.
  • The ST-Link/V2 electronic interface needed for uploading the compiled code to the Blue Pill. Bear in mind that the ST-Link/V2 requires four female-to-female DuPont wires.
  • A green, yellow, and red LED.
  • Three 220-ohm resistors rated at one-quarter...

Understanding sensors

In this section, we explain what sensors are and the importance of sensor modules. It is important to understand first what sensors are and what their purpose is before you use them in practical microcontroller board applications, for example, using photoresistors, which are a useful type of sensor. This information about sensors is essential for further sections.

Defining sensors

A sensor is an electronic component, device, or module that measures physical input from an environment or a particular situation (for example, a sensor measuring water temperature in a fish tank). Sensors are useful for detecting changes in physical variables, such as humidity, temperature, light, vibrations, and movement, among others. Those physical variations are manifested in the sensors by changing their electric/electronic properties, such as changes in resistance and conductivity in the sensors.

There are different types of sensors with different applications. For example...

Introducing photoresistors

This section introduces you to photoresistors, which are very useful for many applications, for example, for measuring the amount of light. In this chapter, we will define what photoresistors are, their classification, and how they are connected to an electronic circuit.

A photoresistor is an electronic component made with light-sensitive material, changing its resistance according to the amount of visible light that it detects. There are different types of photoresistors. Some of them detect ultraviolet (UV) light and others detect infrared light. The latter is used in TV sets, where its infrared light sensor receives data from a remote control. Figure 4.2 shows a common photoresistor used in the examples of this chapter. The photoresistor used in this chapter detects regular light that humans can see. It does not detect infrared nor UV light.

Figure 4.2 – A typical photoresistor

Figure 4.2 – A typical photoresistor

From Figure 4.2, you can see that the...

Connecting a photoresistor to a microcontroller board port

This section shows how to connect a photoresistor to the Blue Pill and the Curiosity Nano boards to read the amount of light from an environment (for example, a living room).

In this section, we also explain how to use three LEDs to indicate that a room is well illuminated by turning a green LED on, that the light is dim by turning a yellow LED on, or that it is dark by turning a red LED on. The electronic circuit with the photoresistor sensor can be placed close to a plant and the circuit can be useful to know whether a plant needs more light or not. The next section shows how to build the circuit with the Blue Pill board.

Connecting a photoresistor to a Blue Pill board

The connection of a photoresistor sensor to a Blue Pill microcontroller board is simple. It can be directly connected to an input analog port, provided that you use a pull-down resistor. The circuit shown in Figure 4.4 describes how to do it, and it...

Coding the photoresistor values and setting up ports

This section shows how to code a Blue Pill application for reading data from a photoresistor.

The following code should run on the microcontroller board circuit shown in Figure 4.4 and Figure 4.5:

int photoresistorPin = PB1;
int photoresistorReading;
void setup(void) {
    pinMode(PB12, OUTPUT); 
    pinMode(PB14, OUTPUT); 
    pinMode(PB15, OUTPUT); 
}
void loop(void) {
    photoresistorReading = analogRead(photoresistorPin); 
    digitalWrite(PB12, LOW);
    digitalWrite(PB14, LOW);
    digitalWrite(PB15, LOW);
    if (photoresistorReading < 600) { 
        digitalWrite(PB15, HIGH);
    } else if (photoresistorReading < 1000) {  
        digitalWrite...

Testing out the photoresistor

This section focuses on how to test out a photoresistor to see if it is working OK. First of all, remember that the photoresistor used in this chapter does not have polarity, so you can safely connect any of its pins (legs) to a microcontroller board's input port.

You also need to make sure that the pull-down resistor connected to the photoresistor has the right value. For example, the pull-down resistor used in the Blue Pill example from Figure 4.4 is 10K ohm, and we used a 220-ohm resistor for the Curiosity Nano example from Figure 4.6. We found those resistor values experimentally. You can try out different resistors connected to the photoresistor to see if the voltage passing through the photoresistor changes widely. Ideally, that voltage should be changing between 0 and 3.3 volts, or close to those values, because in our circuit examples from this chapter, we connected one pin of the photoresistor to 3.3 volts.

In order to see if the photoresistor...

Summary

In this chapter, we learned what sensors are and their applications in electronic projects. This is important because we will continue applying sensors in other chapters from this book. We also defined photoresistors, and how they are classified. We also learned how to connect a photoresistor sensor module to the Blue Pill and Curiosity Nano boards' input ports, and how to analyze and use analog data from a photoresistor.

In this chapter, you learned important pieces of information. We have defined what a sensor and a photosensor is. You can now read data from them using microcontroller boards. The chapter also described how to connect a photosensor module to a microcontroller board.

Chapter 5, Humidity and Temperature Measurement, will explain what a humidity and temperature sensor is and how can we acquire and use its analog data with both the Blue Pill and Curiosity Nano microcontroller boards. This can have a number of applications, such as measuring the temperature...

Further reading

  • Horowitz, P., Hill, W. (2015), The art of electronics [3rd ed.], Cambridge University Press: New York, NY.
  • Microchip (2019), PIC16F15376 Curiosity Nano Hardware User Guide, Microchip Technology, Inc. Available from: http://ww1.microchip.com/downloads/en/DeviceDoc/50002900B.pdf
  • Mims, F.M. (2000), Getting started in electronics, Lincolnwood, IL: Master Publishing, Inc.
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