Add initial files for the HH Hacking 101 - code and infos about board will follow soon
This commit is contained in:
9
code-snippets/ADC_Photo_Resistor.py
Normal file
9
code-snippets/ADC_Photo_Resistor.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from machine import ADC, Pin
|
||||
from time import sleep
|
||||
|
||||
adc = ADC(Pin(34)) # create an ADC object acting on a pin
|
||||
adc.atten(ADC.ATTN_11DB) # 3.3V max
|
||||
|
||||
while True:
|
||||
print("Analog Value: " , adc.read())
|
||||
sleep(1)
|
||||
55
code-snippets/Button_LED_Test.py
Normal file
55
code-snippets/Button_LED_Test.py
Normal file
@@ -0,0 +1,55 @@
|
||||
## Simple Button and LED test
|
||||
# yellow led will blink
|
||||
# red + green will be off button is pressed
|
||||
|
||||
from machine import Pin
|
||||
import time
|
||||
|
||||
## define led pins
|
||||
led_gr_p = 21
|
||||
led_re_p = 22
|
||||
led_ye_p = 17
|
||||
|
||||
## define switch pins
|
||||
sw1_p = 32
|
||||
sw2_p = 33
|
||||
|
||||
|
||||
## init leds
|
||||
led_gr = Pin(led_gr_p, Pin.OUT)
|
||||
led_gr.off() # switch on led - inverted logic!
|
||||
|
||||
led_re = Pin(led_re_p, Pin.OUT)
|
||||
led_re.off() # switch on led - inverted logic!
|
||||
|
||||
led_ye = Pin(led_ye_p, Pin.OUT)
|
||||
led_ye.off() # switch on led - inverted logic!
|
||||
|
||||
|
||||
## int buttons (pull-up)
|
||||
sw1 = Pin(sw1_p, Pin.IN)
|
||||
sw2 = Pin(sw2_p, Pin.IN)
|
||||
|
||||
while True:
|
||||
if sw1.value():
|
||||
print("sw1 true")
|
||||
led_re.on()
|
||||
#led_ye.on()
|
||||
else:
|
||||
print("sw1 false")
|
||||
led_re.off()
|
||||
#led_ye.off()
|
||||
if sw2.value():
|
||||
print("sw2 true")
|
||||
led_gr.on()
|
||||
else:
|
||||
print("sw2 false")
|
||||
led_gr.off()
|
||||
|
||||
# toggle led
|
||||
if led_ye.value():
|
||||
led_ye.off();
|
||||
else:
|
||||
led_ye.on();
|
||||
time.sleep(0.5)
|
||||
|
||||
83
code-snippets/KeyPad_4x4_esp32.py
Normal file
83
code-snippets/KeyPad_4x4_esp32.py
Normal file
@@ -0,0 +1,83 @@
|
||||
# KeyPad Vers #2
|
||||
# Tony Goodhew 9th May 2020
|
||||
# https://www.instructables.com/Using-a-4x4-KeyPad-With-CircuitPython/
|
||||
#
|
||||
# Changes by Christopher Scheuring 2022-05-13 for ESP32 support
|
||||
#
|
||||
# Connections left to right on KeyPad
|
||||
# 0 1 2 3 4 5 6 7
|
||||
# 13 12 14 27 15 02 04 16
|
||||
|
||||
from machine import Pin
|
||||
import gc
|
||||
import time
|
||||
|
||||
gc.collect() # make some room
|
||||
|
||||
# Set up LED on pin 17
|
||||
led_pin = 17
|
||||
|
||||
led = Pin(led_pin, Pin.OUT)
|
||||
led.on() # switch off led - inverted logic!
|
||||
|
||||
# Set up Rows
|
||||
rows = []
|
||||
for p in [13, 12, 14, 27]:
|
||||
row = Pin(p, Pin.OUT)
|
||||
rows.append(row)
|
||||
# anodes OFF
|
||||
for i in range(4):
|
||||
rows[i].off()
|
||||
|
||||
# Set up columns
|
||||
cols = []
|
||||
for p in [15, 02, 04, 16]:
|
||||
col = Pin(p, Pin.IN, Pin.PULL_DOWN)
|
||||
cols.append(col)
|
||||
|
||||
def getkey(): # Returns -999 or key value
|
||||
values = [1,2,3,10, 4,5,6,11, 7,8,9,12, 14,0,15,13]
|
||||
val = -999 # Error value for no key press
|
||||
for count in range(10): # Try to get key press 10 times
|
||||
for r in range(4): # Rows, one at a time
|
||||
rows[r].on() # row HIGH
|
||||
for c in range(4): # Test columns, one at a time
|
||||
if cols[c].value() == 1: # Is column HIGH?
|
||||
p = r * 4 + c # Pointer to values list
|
||||
val = values[p]
|
||||
count = 11 # This stops looping
|
||||
led.off() # Flash LED ON if key pressed
|
||||
rows[r].off() # row LOW
|
||||
time.sleep(0.2) # Debounce
|
||||
led.on() # LED OFF
|
||||
return val
|
||||
|
||||
def getvalue(digits): # Number of digits
|
||||
result = 0
|
||||
count = 0
|
||||
while True:
|
||||
x = getkey()
|
||||
if x != -999 and x < 10: # Check if numeric key pressed
|
||||
result = result * 10 + x
|
||||
print(result)
|
||||
count = count + 1
|
||||
if count == digits:
|
||||
return result
|
||||
|
||||
# +++++ Main +++++
|
||||
print("Press 4 numeric (BLUE) keys")
|
||||
y = getvalue(4)
|
||||
print('\n',y)
|
||||
|
||||
print("\nPress any of the keys\n # halts the program")
|
||||
|
||||
characters =["1","2","3","4","5","6","7","8","9","A","B","C","D","*","#","0"]
|
||||
running = True
|
||||
while running:
|
||||
x = getkey()
|
||||
if x != -999: # A key has been pressed!
|
||||
print(characters[x-1])
|
||||
if x == 15:
|
||||
running = False
|
||||
|
||||
led.on()
|
||||
51
code-snippets/SD_Card_ESP32.py
Normal file
51
code-snippets/SD_Card_ESP32.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from machine import Pin, SPI
|
||||
import machine, sdcard, os, esp32
|
||||
|
||||
spi = SPI(2, baudrate=10000000, polarity=0, phase=0, sck=machine.Pin(18), mosi=machine.Pin(23), miso=machine.Pin(19))
|
||||
sd = sdcard.SDCard(spi, machine.Pin(5))
|
||||
|
||||
os.mount(sd, '/sd')
|
||||
|
||||
def print_directory(path, tabs = 0):
|
||||
for file in os.listdir(path):
|
||||
stats = os.stat(path+"/"+file)
|
||||
filesize = stats[6]
|
||||
isdir = stats[0] & 0x4000
|
||||
|
||||
if filesize < 1000:
|
||||
sizestr = str(filesize) + " byte"
|
||||
elif filesize < 1000000:
|
||||
sizestr = "%0.1f KB" % (filesize/1000)
|
||||
else:
|
||||
sizestr = "%0.1f MB" % (filesize/1000000)
|
||||
|
||||
prettyprintname = ""
|
||||
for i in range(tabs):
|
||||
prettyprintname += " "
|
||||
prettyprintname += file
|
||||
if isdir:
|
||||
prettyprintname += "/"
|
||||
print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr))
|
||||
|
||||
# recursively print directory contents
|
||||
if isdir:
|
||||
print_directory(path+"/"+file, tabs+1)
|
||||
|
||||
#with open("/sd/test1.txt", "w") as f:
|
||||
# f.write("Hello world!\r\n")
|
||||
|
||||
print("Files on filesystem:")
|
||||
print("====================")
|
||||
print_directory("/sd")
|
||||
|
||||
#
|
||||
#print("Files on filesystem:")
|
||||
#print("====================")
|
||||
#print_directory("/sd")
|
||||
#
|
||||
#with open("/sd/test/test1.txt", "w") as f:
|
||||
# f.write("Hello world!\r\n")
|
||||
#
|
||||
#print("Files on filesystem:")
|
||||
#print("====================")
|
||||
#print_directory("/sd/test")
|
||||
Reference in New Issue
Block a user