The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Amrat C, 2023-02-22 03:00:50

Master-Arduino-without-any-Hardware

Master-Arduino-without-any-Hardware

51 www.eTechnophiles.com Program int sensorValue = 0; void setup() { pinMode(A0, INPUT); Serial.begin(9600); } void loop() { // read the input on analog pin 0: sensorValue = analogRead(A0); // print values to the serial monitor Serial.println(sensorValue); delay(10); // Delay a little bit to improve simulation performance } Note: Keep in mind that the Serial Monitor and Serial Plotter in Tinkercad can only display data that is being sent from the Arduino board inside the Tinkercad simulation. They will not display data from real-world devices or circuits. 5. Pushbutton with Arduino Interfacing a push button with Arduino in Tinkercad is a straightforward process that allows you to control the behavior of a circuit using inputs from a button. Circuit Diagram Create the circuit diagram as shown below. You can also copy mine from here. Pushbutton with Arduino


52 www.eTechnophiles.com 1. Open Tinkercad and create a new circuit. 2. Add an Arduino board and a push button to the circuit. 3. Connect one of the legs of the push button to a digital pin on the Arduino board and the other leg to the ground with a 10k ohm pulldown resistor. 4. Connect the LED to digital pin 13 of Arduino with a 220-ohm resistor. 5. Click the "Code" button to open the code editor. 6. Write an Arduino sketch that will detect when the button is pressed and then perform some action, such as turning an LED on or off. This can be done using the digitalRead function to read the state of the pin connected to the button and the digitalWrite function to control the LED. 7. Click the "Circuits" button to return to the circuit view. 8. Click the "Start Simulation" button to run the simulation. 9. Use the mouse to click the button in the simulation to see how it affects the behaviour of the circuit. Program const int buttonPin = 2; const int ledPin = 13; void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); } void loop() { int buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); delay(5000); } else { digitalWrite(ledPin, LOW); } } This code will turn the LED on when the button is pressed and turn it off after 5sec when the button is not pressed. Using the digitalRead and digitalWrite functions, you can easily control the circuit's behavior based on inputs from the button. With these steps, you should easily interface a push button with an Arduino in Tinkercad.


53 www.eTechnophiles.com 6. Control LED brightness with a potentiometer In this project we are going to control the brightness of the LED using a potentiometer with Arduino. But what is potentiometer? Remember those turning knobs on radios and stereo? They are actually potentiometers! What is a potentiometer? Circuit Diagram Create the circuit diagram as shown below. You can also copy mine from here. Control LED brightness with a potentiometer 1. Open Tinkercad and create a new circuit. 2. Add a breadboard for connections. 3. Add an Arduino board and a potentiometer to the circuit. 4. Connect one of the legs of the potentiometer to 5V, another leg to the ground, and the middle leg to an analog pin on the Arduino board. 5. Connect the LED to the 9th digital pin of Arduino with a 220ohm resistor. 6. Click the "Code" button to open the code editor. 7. Write an Arduino sketch that will read the analog input from the potentiometer and use it to control some aspect of the circuit, such as the brightness of an LED. This can be done using the


54 www.eTechnophiles.com analogRead function to read the voltage on the analog pin and the analogWrite function to control the LED. 8. Click the "Circuits" button to return to the circuit view. 9. Click the "Start Simulation" button to run the simulation. 10. Use the mouse to adjust the position of the potentiometer in the simulation to see how it affects the behavior of the circuit. Program // This line sets the potPin constant to analog pin A0 const int potPin = A0; // This line sets the ledPin constant to digital pin 9 const int ledPin = 9; void setup() { // This line sets the mode of the ledPin to OUTPUT pinMode(ledPin, OUTPUT); } void loop() { // This line reads the value from the analog pin potPin and stores it in the potValue variable int potValue = analogRead(potPin); // This line maps the value from the potentiometer (0 to 1023) to the value for the LED (0 to 255) int ledValue = map(potValue, 0, 1023, 0, 255); // This line sets the brightness of the LED using PWM analogWrite(ledPin, ledValue); } This program will adjust the brightness of the LED based on the position of the potentiometer. The analogRead function reads the voltage from the analog pin. The map function scales this value from 0-1023 to the range of 0-255. This is necessary because the analogwrite function only accepts values between 0-255. With these steps, you should be able to interface a potentiometer with an Arduino in Tinkercad with ease. 7. How to use an LDR with Arduino A Light Dependent Resistor (LDR) allows you to control the behavior of a circuit based on the ambient light level.


55 www.eTechnophiles.com Circuit Diagram Create the circuit diagram as shown below. You can also copy mine from here. How to use an LDR with Arduino 1. Open Tinkercad and create a new circuit. 2. Add a breadboard for making all connections 3. Add an Arduino board and an LDR to the circuit. 4. Connect one of the legs of the LDR to 5V, another leg to the ground, and the middle leg to an analog pin on the Arduino board. 5. Add a resistor to the circuit and connect it in series with the LDR. The value of the resistor should be between 10KΩ and 100KΩ. 6. Connect an LED to the 9th digital pin of Arduino with a 220ohm resistor. 7. Click the "Code" button to open the code editor. 8. Write an Arduino sketch that will read the analog input from the LDR and use it to control some aspect of the circuit, such as the brightness of an LED. This can be done using the analogRead function to read the voltage on the analog pin and the analogWrite function to control the LED. 9. Click the "Circuits" button to return to the circuit view. 10. Click the "Start Simulation" button to run the simulation. 11. Use the mouse to adjust the ambient light level in the simulation to see how it affects the behavior of the circuit.


56 www.eTechnophiles.com Program // This line sets the ldrPin constant to analog pin A0 const int ldrPin = A0; // This line sets the ledPin constant to digital pin 9 const int ledPin = 9; void setup() { // This line sets the mode of the ledPin to OUTPUT pinMode(ledPin, OUTPUT); } void loop() { // This line reads the value from the analog pin ldrPin and stores it in the ldrValue variable int ldrValue = analogRead(ldrPin); // This line maps the value from the LDR sensor (0 to 1023) to the value for the LED (0 to 255) int ledValue = map(ldrValue, 0, 1023, 0, 255); // This line sets the brightness of the LED using PWM analogWrite(ledPin, ledValue); } This program adjusts the brightness of the LED based on the ambient light level as measured by the LDR. The analogRead function reads the voltage on the analog pin, and the map function scales this value from the range of 0-1023 to the range of 0-255, which is required by the analogWrite function. With these steps, you should be able to interface an LDR with an Arduino in Tinkercad with ease. This is just one example of how you can use analog inputs to control the behavior of a circuit, and you can use this same basic approach to interface other types of analog sensors and inputs with an Arduino in Tinkercad. 8. Temperature measurement using Arduino Interfacing a temperature sensor with an Arduino in Tinkercad is a simple process that allows you to measure the ambient temperature and control the circuit's behavior based on this measurement. Circuit Diagram Create the circuit diagram as shown below. You can also copy mine from here.


57 www.eTechnophiles.com Temperature measurement using Arduino 1. Open Tinkercad and create a new circuit. 2. Add an Arduino board and a temperature sensor to the circuit. 3. Connect the power pin of the temperature sensor to 5V, the ground pin to the ground, and the data pin to an analog pin on the Arduino board. 4. Connect LED to digital pin 9 of Arduino with 220 ohm resistor. 5. Click the "Code" button to open the code editor. 6. Write an Arduino sketch that will read the temperature from the sensor and use it to control some aspect of the circuit, such as the brightness of an LED. This can be done using the analogRead function to read the voltage on the analog pin and the analogWrite function to control the LED. 7. Click the "Circuits" button to return to the circuit view. 8. Click the "Start Simulation" button to run the simulation. 9. Use the mouse to adjust the temperature in the simulation to see how it affects the behavior of the circuit. Program


58 www.eTechnophiles.com // Constants to store the pin numbers for the temperature sensor and LED const int tempPin = A0; const int ledPin = 9; // The setup function runs once when the program starts void setup() { // Set the LED pin as an output pinMode(ledPin, OUTPUT); } // The loop function runs repeatedly in an infinite loop void loop() { // Read the temperature from the sensor and store it in a variable int tempValue = analogRead(tempPin); // Map the temperature value to a value between 0 and 255 int ledValue = map(tempValue, 0, 1023, 0, 255); // Write the mapped value to the LED pin analogWrite(ledPin, ledValue); } This code will adjust the brightness of the LED based on the ambient temperature as measured by the temperature sensor. The analogRead function is used to read the voltage on the analog pin, and the map function is used to scale this value from the range of 0-1023 to the range of 0- 255, which is required by the analogWrite function. The End Hey, I hope this ebook served its purpose of training you to learn Arduino using Tinkercad. Since it’s a free version, there are only 8 projects added as of now. The final version is under progress and will have 30+ projects, complete electronics course and much more. Sign up to our newsletter to get the latest updates about it right in your inbox!


Click to View FlipBook Version