WEEK 2

Module 2

For this module,learned about electric circuit principles where button switch,buzzers,PWM,Potentiometer and LED's whereby interfacing each component.Used each spreadsheet to have a better understanding on the components.Resistors used pull up for the current value to be 1V for the loop and pull down to be 0v.Potential divider to know the voltage in each loop,the impendance and current limiting.

Interfacing a LED with a button switch

from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
if button.value():
led.toggle()
time.sleep(0.5)
button.led

Interfacing a LED with a potentiometer to vary the brightness which is an analogue input.

Projects done after incorporating the components was traffic and binary lights.Traffic lights uisng a Red,Amber and Green LED's while for binary counter(3^2) threebit_counter.

Traffic light

from machine import Pin
from time import sleep
# traffict lights(red, amber, green)
red = Pin(13,Pin.OUT)
amber = Pin(14,Pin.OUT)
green = Pin(15,Pin.OUT)
while True:
 gred.on()
 amber.off()
 green.off()
 sleep(.5)
 red.off()
 amber.on()
 green.off()
 sleep(.5)
 red.off()
 amber.off()
 green.on()
 sleep(.5)

three binary counter

from machine import Pin
from time import sleep
red = Pin(15, Pin.OUT)
amber = Pin(13, Pin.OUT)
green = Pin(11, Pin.OUT)
while True:
red.off()
amber.off()
green.off()
sleep(.1)
amber.off()
red.off()
green.on()
sleep(1)
red.off()
amber.off()
green.on()
sleep(.1)
red.off()
amber.on()
green.on()
sleep(.1)
red.on()
amber.off()
green.off()
sleep(.1)
red.on()
amber.on()
green.off()
sleep(.1)
red.on()
amber.off()
green.on()
sleep(.1)
red.on()
amber.on()
green.on()

Also,Sensors and actuators where introduced whereby the sensor is a device that produces an output signal for the purpose of sensing a physical phenomenon and an actuator a component of a machine that produces force, torque, or displacement, usually in a controlled way, when an electrical, pneumatic or hydraulic input is supplied to it in a system (called an actuating system).The sensors introduced where ULTRASONIC sensor (HCSR401) and PIR.The HC-SR401 working principle is to emmit a high frequecy signal that is beyond the human hearing(echo) where the transmitter does and the receiver bounces back using a 30 degree angle(trigger).

The HC-SR401 has 4 pins containing the Vcc, Echo, Trigger and Ground.Below image:

Ultrassonic.image

Simulation code

from machine import Pin
from time import sleep
trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
def ultra():
trigger.low()
sleep(2)
trigger.high()
sleep(2)
trigger.low()
while echo.value() == 0:
signaloff = ticks_us()
while echo.value() == 1:
signalon = ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
print("The distance from the objects is",distance,"cm")
while True:
ultra()
sleep(1)
RPI.ultrasonic

Went further for the assignment when oled was used in combo to display the binary counter.For the OLED to be used a Library was install (SSD1306).

from machine import Pin, SPI
from time import sleep
import framebuf
from ssd1306 import SSD1306_SPI
spi = SPI(0, 100000, mosi=Pin(19), sck=Pin(18))
oled = SSD1306_SPI(128, 64, spi, Pin(17), Pin(20), Pin(16))
while True:
oled.text("Hello Victoria",10,28)
oled.show()
OLED display

Threebinary_counter interfacing with OLED

from machine import Pin, SPI
from time import sleepfrom machine import Pin, SPIfrom machine import Pin, SPI
from time import sleep
import framebuf
from ssd1306 import SSD1306_SPI
red = Pin(15, Pin.OUT)
amber = Pin(13, Pin.OUT)
green = Pin(11, Pin.OUT)
spi = SPI(0, 100000, mosi=Pin(19), sck=Pin(18))
oled = SSD1306_SPI(128, 64, spi, Pin(17),Pin(20), Pin(16))
while True:
oled.text("counter 0",30,28)
oled.show()
red.off()
amber.off()
green.off()
sleep(.1)
oled.fill(0)
oled.show()
amber.off()
red.off()
green.on()
sleep(.1)
oled.text("counter 1",30,28)
oled.show()
sleep(1)
oled.fill(0)
oled.show()
red.off()
amber.off()
green.on()
oled.text("counter 2",30,28)
oled.show()
sleep(.1)
oled.fill(0)
oled.show()
red.off()
amber.on()
green.on()
oled.text("counter 3",30,28)
oled.show()
sleep(.1)
oled.fill(0)
oled.show()
red.on()
amber.off()
green.off()
oled.text("counter 4",30,28)
oled.show()
sleep(.1)
oled.fill(0)
oled.show()
red.on()
amber.on()
green.off()
oled.text("counter 5",30,28)
oled.show()
sleep(.1)
oled.fill(0)
oled.show()
red.on()
amber.off()
green.on()
oled.text("counter 6",30,28)
oled.show()
sleep(1)
oled.fill(0)
oled.show()
red.on()
amber.on()
green.on()
oled.text("counter 7",30,28)
oled.show()
sleep(1)
from machine import Pin, SPI
from time import sleep
import framebuf
from ssd1306 import SSD1306_SPI
red = Pin(15, Pin.OUT)
amber = Pin(13, Pin.OUT)
green = Pin(11, Pin.OUT)