Arduino Lesson 17 updated code and info
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Arduino Lesson 17 updated code and info

by Blueshark on Wed Feb 06, 2019 12:16 am

if anyone is or has done Arduino Lesson 17, info needs to be added to the lesson...

heres the lesson...

What you need to know is that it works, theres just a few things that need a little more explanation to get it working. The code below for the Python file is up and ready, it will work; use this one instead of the lesson 17 version. The Arduino code works as is no need to modify unless you add your own code.

I'm using Ubuntu Linux 16.04 and have Python2 and Python3 installed
This code if for Python2
when calling this code from the terminal/command prompt since I have both Python versions I had to call Python2 like this to get it working "python2 movement.py" and off it goes.... if you only have Python2 installed you can just call "python movement.py" its the default...

and if your a Windows user you need to change this line of code

Code: Select all | TOGGLE FULL SIZE
   ser = serial.Serial('/dev/ttyACM0', 9600)

To whatever COM# your Arduino is using example like this...

Code: Select all | TOGGLE FULL SIZE
   ser = serial.Serial('COM3', 9600)


Use this code and name it “movement.py”

Code: Select all | TOGGLE FULL SIZE
import time
import serial
import smtplib
TO = 'receiving gmail address'
GMAIL_USER = 'another diffirent gmail address'
GMAIL_PASS = 'your password'
SUBJECT = 'Intrusion!!'
TEXT = 'Your PIR sensor detected movement'
ser = serial.Serial('/dev/ttyACM0', 9600)
def send_email():
    print("Sending Email")
    smtpserver = smtplib.SMTP("smtp.gmail.com",587)
    smtpserver.login(GMAIL_USER, GMAIL_PASS)
    header = 'To:' + TO + '\n' + 'From: ' + GMAIL_USER
    header = header + '\n' + 'Subject:' + SUBJECT + '\n'
    print (header)
    msg = header + '\n' + TEXT + ' \n\n'
    smtpserver.sendmail(GMAIL_USER, TO, msg)
while True:
    message = ser.readline()
    if message[0] == 'M' :

Just add your receiving gmail address, and a diffirent gmail address and place its password in the password section.
(This code only works with gmail addresses....)

Just one more thing you need to do before you get going!!!!

The sending gmail that includes the password in the code needs to let its security down...... Google prevents low security devices from using it/or accessing its gmail service.

To get over this security problem you need to go to the gmail user settings

------ go to security tab/section

------- look for the " Less secure app access " window; its set to OFF.... you need to set it to ON

and thats it.... your cooking....

This code is interesting because you can add more sensors to this...

Enjoy, have fun...


Posts: 124
Joined: Thu Dec 21, 2017 7:19 pm

Re: Arduino Lesson 17 updated code and info

by adafruit_support_bill on Wed Feb 06, 2019 6:44 am

Thanks for the feedback. I will forward it to the authors of the guide.

Posts: 72771
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Lesson 17 updated code and info

by Blueshark on Wed Feb 06, 2019 9:35 pm

adafruit_support_bill …… can you include this Arduino code

This Arduino code will work on the Ultra Sonic Distance Sensor instead of the PIR Sensor.


To read the distance in Inches (its the default) or Centimeters, just comment out the code where I point it out; don't forget to comment it back or else you will send a bunch of data to the serial port for the Python code to look at.

Code: Select all | TOGGLE FULL SIZE
const int pingPin = 7;  // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6;  // Echo Pin of Ultrasonic Sensor
// Using Ultrasonic Distance Sensor Model "HC-SR04"
int minSecsBetweenEmails = 60; // 1 min
long lastSend = -minSecsBetweenEmails * 1000l;
void setup()
  Serial.begin(9600);  // Starting Serial Terminal
void loop()
  //----------- variables ----------------------------------
  long now = millis();
  long duration, inches, cm;

  //--- ultrasonic distance sensor code --------------------
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin, HIGH);
  digitalWrite(pingPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  //uncomment these lines to see distance in Arduino Serial Monitor
  // then comment them back so you wont send too much data to the serial port
  // to use with the python code

  if(inches<=30 && inches>=1)// distance in which the Distance Sensor is set for alarm.
  // if you want to use Centimeters replace "inches" to "cm" in the "if" statement
    if (now > (lastSend + minSecsBetweenEmails * 1000l))
      lastSend = now;
      Serial.println("Too soon");
  delay(100); // original delay was 500

//---------- Ultrasonic Distance Sensor Math conversions ---

long microsecondsToInches(long microseconds){
  // 73.746 microseconds per "INCH" (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the soundwave, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  return microseconds / 74 / 2;

long microsecondsToCentimeters(long microseconds){
  // The speed of sound is 340 m/s or 29 microseconds per "CENTEMETER".
  // The sound travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;


Posts: 124
Joined: Thu Dec 21, 2017 7:19 pm

Please be positive and constructive with your questions and comments.