Writing the Arduino sketch

In the Arduino world, programs that you upload to the microcontroller are called sketches. You can write these sketches in a free integrated development environment (IDE) which can be downloaded from here:
https://www.arduino.cc/en/Main/Software

Once you finalize a program, you upload it to your Arduino board using the upload button on the IDE and voila: your board starts doing what you asked it to do.

Every Arduino sketch will have two methods where you get to define the logic of your program:

  • setup(): For one-time initialization
  • loop(): For things that the board keeps doing forever until it runs out of power

Here's the code that we upload to the Arduino (see 9.13.arduino_sketch.ino):

const int triggerPin = 8;
const int echoBackPin = 7;

void setup() {
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoBackPin, INPUT);
}

void loop() {
long duration, distanceIncm;
// trigger ultrasound ping
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(5);
digitalWrite(triggerPin, LOW);
// receive input from the sensor
duration = pulseIn(echoBackPin, HIGH);

//calculate distance
distanceIncm = duration / 29 / 2;

// send data over serial port
Serial.print(distanceIncm);
Serial.println();
delay(100);
}

The code description is as follows:

  • The first two lines indicates that we will use pin numbers 7 and 8 on the Arduino board and we assign them the variable names triggerPin and echoBackPin.
  • The setup function initializes the serial port and fixes its baud rate at 9600. Baud rate is defined as the number of signal changes that occur in a second. We will use the same rate when reading data in Tkinter with Python.
  • The code pinMode(triggerPin, OUTPUT) means that we will now use Pin 8 to send an output pulse to the sensor. 
  • Similarly, the code pinMode(echoBackPin, INPUT); declares that we will use Pin 7 to receive input from the sensor.
  • Within the loop, we start by setting pin triggerPin to low pulse.  We then trigger the sensor to emit ultrasound by triggering a high voltage pulse of 2 microseconds. This triggers the sensor to emit an ultrasound for 5 microseconds. We then mark the pin LOW to stop triggering the ultrasound pulse.
  • We then time the signal received on echoBackPin using duration = pulseIn(ioPin, HIGH). This gives us the time (in microseconds) it took for the ultrasound to reflect back.
  • Given that the speed of sound is 340 m/s or 29 microseconds per centimeter, we find the distance using the formula distance = speed * time. But since this is the time it took for a reflected sound to travel out and back, the actual distance is half this value. Perhaps the math should be done by Python instead? Doing division here using a long method will result in a whole number and so will not be precise. Note that we could have also offloaded this calculation from Arduino to our Python code, as most Arduino processors do not directly support floats in hardware, and doing so in software on such a limited processor could bog it down. 
  • The line delay(100) ensures that the previous code runs every 100 milliseconds, sending pulses of ultrasound and measuring the distance to whatever the sensor is pointed at.

The moment this code is uploaded to the Arduino board, it starts sending 5-microsecond pulses of ultrasound after a delay of 100 milliseconds. It also sends a message to the serial port of your computer in every one of these loops.

Now it's time to read this using Python and then display it in a Tkinter widget.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.7.131