Tinkercad - Sensor de proximidad

3 min read 1 month ago
Published on Sep 05, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will explore how to use a proximity sensor to control two motors with Tinkercad. This project is perfect for beginners looking to enhance their understanding of electronics and programming with Arduino. By the end of this guide, you'll be able to integrate a proximity sensor into your projects and control motors based on detected distances.

Step 1: Setting Up Tinkercad

  • Go to the Tinkercad website and log in or create a new account.
  • Start a new project by selecting "Create New Circuit."
  • Familiarize yourself with the Tinkercad interface, which includes a workspace where you will build your circuit.

Step 2: Adding Components

  • Drag the following components from the components panel into your workspace:
    • Arduino Uno
    • Proximity Sensor (such as the HC-SR04)
    • Two DC Motors
    • Motor Driver (such as L298N)
    • Breadboard
    • Jumper Wires
    • Power Supply (if necessary)

Step 3: Wiring the Proximity Sensor

  • Connect the VCC pin of the proximity sensor to the 5V pin on the Arduino.
  • Connect the GND pin of the sensor to the GND pin on the Arduino.
  • Connect the Trig pin of the sensor to a digital pin on the Arduino (e.g., pin 9).
  • Connect the Echo pin of the sensor to another digital pin on the Arduino (e.g., pin 10).

Step 4: Wiring the Motors

  • Connect the motors to the motor driver:
    • Motor A to the outputs of the motor driver (e.g., OUT1 and OUT2).
    • Motor B to another set of outputs (e.g., OUT3 and OUT4).
  • Connect the motor driver's input pins to the Arduino:
    • Input 1 and Input 2 for Motor A (e.g., pins 3 and 4).
    • Input 3 and Input 4 for Motor B (e.g., pins 5 and 6).
  • Connect the motor driver’s VCC to the power supply and GND to the Arduino GND.

Step 5: Writing the Code

  • Open the code editor in Tinkercad.
  • Use the following code to read the proximity sensor and control the motors based on distance:
#define trigPin 9
#define echoPin 10
#define motorA1 3
#define motorA2 4
#define motorB1 5
#define motorB2 6

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  long duration, distance;
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034) / 2;
  
  if (distance < 20) { // If an object is closer than 20 cm
    digitalWrite(motorA1, HIGH);
    digitalWrite(motorA2, LOW);
    digitalWrite(motorB1, HIGH);
    digitalWrite(motorB2, LOW);
  } else {
    digitalWrite(motorA1, LOW);
    digitalWrite(motorA2, LOW);
    digitalWrite(motorB1, LOW);
    digitalWrite(motorB2, LOW);
  }
  delay(100);
}

Step 6: Testing Your Circuit

  • Click on the "Start Simulation" button in Tinkercad.
  • Observe the motors' behavior when objects are placed at varying distances from the proximity sensor.
  • Adjust the distance threshold in the code if needed to fit your specific use case.

Conclusion

In this tutorial, you learned how to set up a proximity sensor with Tinkercad to control two motors. You gained hands-on experience with wiring and programming, which will be valuable for future projects. Consider experimenting with different types of sensors or motors to expand your skills further. Happy tinkering!