Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 176 - CodePilot V5.0 C7
P. 176

    void setup(): This is a built-in function in Arduino programming. The code inside the setup()
                         function runs once when the program starts. It is used for an initial setup, like defining pin
                         modes.

                          pinMode(7, INPUT): This line sets pin 7 as an input. This means that pin 7 will read signals,
                         specifically from the light sensor (photoresistor), to detect if it is dark or light.
                          Serial.begin(9600): This initialises serial communication at a baud rate of 9600, allowing the
                         Arduino to send data to the serial monitor for debugging or monitoring purposes.

                          pinMode(8, OUTPUT): This line sets pin 8 as an output. Pin 8 will send signals to control the
                         LED or smart lamp.

                          void loop(): The loop() function runs repeatedly after the setup() function. It continuously
                         reads the sensor data and controls the lamp based on whether it is light or dark.
                          Nightsensor  =  digitalRead(7): This line reads the value from the light sensor (photoresistor)

                         connected to pin 7. If it is dark, the sensor returns 0; if it is light, the sensor returns 1.
                          Serial.println(Nightsensor): This prints the value of the Nightsensor to  the serial monitor.
                         It shows whether the sensor detects light (1) or dark (0).

                          if (Nightsensor == 0): This condition checks if it is dark (Nightsensor == 0 means it’s dark).
                         If true, it turns on the LED (lamp).

                          digitalWrite(8, HIGH): If it is dark, this sends a HIGH signal to pin 8, turning on the LED or lamp
                         connected to that pin.
                          else: If the sensor detects light (Nightsensor == 1), it proceeds to the else block.

                          digitalWrite(8, LOW): This sends a LOW signal to pin 8, turning off the LED or lamp.
                          delay(10): This pauses the program for 10 milliseconds, allowing the system to work without

                         running too quickly and improving simulation performance.
                      After completing all the steps and connecting the components, your smart lamp will be ready to
                      automatically turn on when it’s dark and off when it’s light. You have successfully built a simple,

                      energy-efficient system using Arduino and sensors.

                                                                 Ask
                                                                           AGENT
                                                            OrangeAI
                        Build a circuit with a temperature sensor connected to Arduino in Tinkercad.

                               Study

                                                                                                      21 st   #Initiative
                           INTERDISCIPLINARY LEARNING                                               Century   #Collaboration
                                                                                                     Skills

                        Discuss how smart circuits, like automatic street lights, help save energy and contribute to the creation
                        of sustainable smart cities. How do these systems function to reduce
                        energy usage and support environmental sustainability?                  social science

                       174
                            CodePilot (V5.0)-VII

| 171 172 173 174 175 176 177 178 179 180 181 |