Activity Handbook - Introduction To Electronics & Basic IoT Principles

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Day 1- Specialized Training

Creative Design and Innovation – C3

Introduction to Electronics & Basic IoT


Principles

Activity Handbook
Activity: Programming PIR module
from time import sleep
from pymata_aio.pymata3 import PyMata3
from pymata_aio.constants import Constants
board = PyMata3(arduino_wait =2)

# declare a variable to hold the PIR pin number that you will connect to.

# declare the mode of the pin ‘PIR’ that we declared in the previous step.
board.set_pin_mode(___,_____)

try:
while True:

# declare a variable to hold the readings and read the input value
value = _______________(__)
print ("reading is: ", value)
#introduce delay of 1 second

except KeyboardInterrupt:
print("\nDone")
board.shutdown()

Activity: Programming LED to blink


from time import sleep
from pymata_aio.pymata3 import PyMata3
from pymata_aio.constants import Constants
board=PyMata3(arduino_wait=1)

# declare a variable to hold the LED pin number that


you will connect to.

# declare the mode of the pin ‘LED’ that we declared


in the previous step
board.set_pin_mode(__,__________)

try:
while True:
board.digital_write(LED,1)
#introduce delay of 0.5 second
#turn the LED OFF
#introduce delay of 0.5 second

except KeyboardInterrupt:
board.shutdown()
print("\n done")

Activity: Controlling LED with Pushbutton


Combine these codes to control the LED with a Pushbutton

from time import sleep


from pymata_aio.pymata3 import PyMata3
from pymata_aio.constants import Constants
board = PyMata3(arduino_wait =2)

#declare a variable to hold the Pushbutton pin number that you will connect to.
#declare a variable to hold the LED pin number that you will connect to.

# declare the mode of the pin ‘LED’ that we declared in the previous step
board.set_pin_mode(___,___________)
# declare the mode of the pin ‘Pushbutton’ that we declared in the previous step
board.set_pin_mode(___,___________)

try:
while True:

# declare a variable to hold the readings and read the input value
value = _______________(__)

if value == 1:
#turn the LED ON
else:
#turn the LED OFF

except KeyboardInterrupt:
print("\nDone")
board.shutdown()

Activity: Automatic Lamp


from time import sleep
from pymata_aio.pymata3 import PyMata3
from pymata_aio.constants import Constants
board = PyMata3(arduino_wait =2)

#declare a variable to hold the LDR pin number that you will connect to.
#declare a variable to hold the LED pin number that you will connect to.

# declare the mode of the pin ‘LED’ that we declared in the previous step
board.set_pin_mode(___,___________)
# declare the mode of the pin ‘LDR’ that we declared in the previous step
board.set_pin_mode(___,___________)

try:
while True:
# declare a variable to hold the readings and read the input value
value = _______________(__)
if value < 200:
#turn the LED ON
else:
#turn the LED OFF

except KeyboardInterrupt:
print("\nDone")
board.shutdown()

Activity: Automatic line following vehicle


from pymata_aio.pymata3 import PyMata3
from pymata_aio.constants import Constants
board = PyMata3(arduino_wait=2)

Lsensor = 4
#Initialise the pin connection for the following devices using the given variable names:

board.set_pin_mode(Lsensor, Constants.INPUT)
# Initialize the connection mode for the following pins:
board.set_pin_mode(Msensor, Constants.INPUT)
board.set_pin_mode(Rsensor, Constants.INPUT)
board.set_pin_mode(MOTORLEN, Constants,PWM)
board.set_pin_mode(MotorL1, Constants.OUTPUT)
board.set_pin_mode(MotorL2, Constants.OUTPUT)
board.set_pin_mode(MotorR1, Constants.OUTPUT)
board.set_pin_mode(MotorR2, Constants.OUTPUT)
board.set_pin_mode(MOTORREN, Constants,PWM)
try:
while True:
Rvalue = board.digital_read(Rsensor)
Mvalue = board.digital_read(Msensor)
Lvalue = board.digital_read(Lsensor)

# read the value from the IR sensors given in the table below.

def forward():
board.digital_write(MotorR1, 1)
board.digital_write(MotorR2, 0)
board.analog_write(MotorRen, 255)
board.digital_write(MotorL1, 1)
board.digital_write(MotorL2, 0)
board.analog_write(MotorLen, 255)

#Write the remaining functions to


• Stop the vehicle

def STOP:
board.digital_write(MotorR1, 0)
board.digital_write(MotorR2, 0)
board.analog_write(MotorRen, 0)
board.digital_write(MotorL1, 0)
board.digital_write(MotorL2, 0)
board.analog_write(MotorLen, 0)

• Steer the vehicle to the right


def right:
board.digital_write(MotorR1, 1)
board.digital_write(MotorR2, 0)
board.analog_write(MotorRen, 255)
board.digital_write(MotorL1, 1)
board.digital_write(MotorL2, 0)
board.analog_write(MotorLen, 100)

• Steer the vehicle to the left


def right:
board.digital_write(MotorR1, 1)
board.digital_write(MotorR2, 0)
board.analog_write(MotorRen, 100)
board.digital_write(MotorL1, 1)
board.digital_write(MotorL2, 0)
board.analog_write(MotorLen, 255)

Complete the following table for the IR sensors readings in the different scenarios. The first
scenario is done for you:
IR sensor state

Left Middle Right Scenario

HIGH/LOW HIGH/LOW HIGH/LOW

LOW HIGH LOW Vehicle on track


LOW LOW HIGH Vehicle drifting right
HIGH LOW LOW Vehicle drifting left
HIGH HIGH HIGH Vehicle has reached the end line (stop)
LOW LOW LOW Vehicle is completely out of the line
try:
while True:
if (Lvalue == 0 and Mvalue == 1 and Rvalue == 0):
forward()
# Complete the code using ‘elif’ statements to check the other.
The scenario for the code given in the example above, is shown in
red in the table below.

except KeyboardInterrupt:
print(“\nDone”)
board.shutdown()

You might also like