2. 7/8/2020 Signal Processing and Radio Communications Lab 2
Content
1. Why Python
2. Raspberry Pi
i. Structure
ii. Installation
iii.GPIO pins
iv.PWM
3. ESP32
i. Structure
ii. Micropython
iii.Installation
iv.GPIO pins
v. PWM
4. Books & Courses
3. 3
Device control and debugging
● Easy in analyzing bus traffic such as USB, SPI or I2C.
● Many bus analyzer and communication tools have user friendly interfaces that can be used to control the tool.
Automating testing
● The ability to control tools that can send and receive messages from an embedded system through Python
opens up the possibility for using Python to create automated tests that include regression testing.
Data analysis
● There are many freely available and powerful libraries.
Real-time Software
● A version for real-time is the Micropython port that is designed to run on microcontrollers.( ARM Cortex-M3/4).
Learning object oriented programming
● Free programming language ,easy to learn.
● Has the ability to be structured in a free-form script type manner or as a sophisticated object oriented
architecture.
1. Why Python ?
5. 5
Raspberry Pi 3 B+ Hardware
● Broadcom BCM2837 SoC
○ 1.4GHz, 1Gb SRAM
● Quad Core
● 40 GPIO pins
● 4 USB ports
● Micro SD card slot
● Bluetooth 4.2/BLE
● Integrated Wi-Fi
● Power over Ethernet(PoE)
More:
https://www.raspberrypi.org/pr
oducts/raspberry-pi-3-model-b-
plus/
2.1 Structure of Raspberry Pi
6. 6
Step 1:
Plug in a monitor (via HDMI) and a keyboard and mouse
(via USB).
● Need an interface to the device.
Step 2: Get an operating system.
● Raspberry Pi needs an OS.
● OS image must be present on the micro SD card.
Refer : https://youtu.be/y45hsd2AOpw
2.2 Installation
7. 7
Installing an Operating System
Use New Out- Of-Box Software (NOOBS)
● Normally comes preinstalled on micro SD bundled with Raspberry Pi boards.
● Otherwise, download it for free from https://projects.raspberrypi.org/en/projects/noobs-install.
● If NOOBS is not preinstalled on micro SD, you’ll need to:
1. Format the micro SD(need an SD reader)
2. Extract the NOOBS download
3. Put it on the micro SD
8. 8
● From this pop up window user can select
any OS , but most recommended one is
Raspbian.
● Like this way NOOBS will install an
operating system on your microSD card.
9. 9
Raspi- Configuration
● Raspi- config is a tool which lets you set up various setup/boot options for the Raspberry
PI.
● It will run automatically when you boot the Raspberry Pi with a new micro SD card for the
first time.
10. 10
❖ Expand Filesystem - Reformats your micro SD card filesystem to allow access to all the memory.
❖ Change User Password - Raspberry Pi starts with one user account ( username: “pi”) with a
default password (“raspberry”).
❖ Enable boot to Desktop/Scratch
● Console is default boot option.
● Desktop is the graphic interface.
● Scratch is a programming environment for kids.
If you use desktop you can invoke console too. But you use console , then you can only get
console.
11. 11
● There are 40 pins in Raspberry Pi 3 B+ model.
● Basically there are,
○ power and ground pins
○ GPIO pins
○ Other multi functional pins
● Anyhow there are 2 ways to number these
pins.
1. GPIO.BOARD
Number of pins in their order on the
board.(as shown under Pin#)
1. GPIO.BCM
The Broadcom SoC number.(
No.shown after GPIO)
● In here we used board numbering system.
https://datasheets.raspberrypi.org/cm/cm3-
plus-datasheet.pdf
2.3 Pins
12. 12
Set pin mode
➔ Set a pin to act as an INPUT or OUTPUT pin.
◆ GPIO.set up (pin no, GPIO.OUT)
Ex: GPIO.set up (13, GPIO.OUT)
◆ GPIO.set up (pin no, GPIO.IN)
Ex: GPIO.set up (13, GPIO.IN)
Pin Direction
➔ Assign a value to pins set as OUTPUT pins. (3.3v / 0 v)
◆ GPIO.output(pin no, True)
Ex:GPIO.output(13, True)
◆ GPIO.output(pin no, False)
Ex:GPIO.output(13, False)
Let’s begin with GPIO pins
13. 7/8/2020 Signal Processing and Radio Communications Lab confidential 13
Blinking an LED
https://youtu.be/PU7i7TSYF40
14. Reading an input pins
GPIO.setup( pin no , GPIO.IN)
value = GPIO.input(pin no.)
● This can read value on input pin.
● But in Raspberry pi there is no analog to digital converter. So it can only read digital
values.
7/8/2020 Signal Processing and Radio Communications Lab confidential 14
15. 15
Period (T) = time for one cycle
Freequency (f) = number of cycles per second
2.4 Pulse Width Modulation (PWM)
f = 1 / T
16. 16
● Pulse - one cycle
● pulse width - HIGH/ON period of pulse
● Raspberry Pi generates only digital signals as high or low. But there are some devices,
which respond to analog. But we can't generate analog from this. So, one way to deal with
that is to send a pulse width modulated signal. User can make the signal go from high
to low really fast.
Duty Cycle = Pulse width / Period * 100%
17. 17
● Imagine these are duty
cycles of pulses for three
iLEDs. In here the pulses
have with same frequency
as well as same time period.
● Only difference is their duty
cycles. It make differences in
LEDs intensity as follows:
L2>L1>L3
https://youtu.be/1fKH5PU9ec
k
1
2
3
18. 18
PWM Initialization
● Mark pin for PWM.
pwm_obj = GPIO.PWM( pin no ,
frequency)
● Start generating PWM signal
according to given duty cycle(0-100).
pwm_obj.start(duty cycle)
PWM Control
● Assign new duty cycle.
pwm_obj.ChangeDutyCycle(50)
Frequency Control
● Need to do it manually.
while True:
GPIO.output( pin no. , True)
time.sleep(0.5)
GPIO.output( pin no. , False)
time.sleep(0.5)
● This will give a 1Hz pulse.
22. 22
● A compact implementation of
python 3.
● Created for usage in
microcontrollers.
● Originally Created by Australian
programmer Damien George.
● Only include subset of python
libraries.
➔ Basically it’s a stripped and trimmed
version of python in order to work
with embedded devices with limited
resources.
➔ Support many devices like ESP32,
ESP8266, PyBoard, Wipy-pycom
3.2 What is micro python?
23. 23
Points to note….
math – mathematical functions (e.g.
sin, pi)
Python standard
lib
MicroPython Libraries
MicroPython and
ESP32 specific lib
sys – system specific functions (e.g.
sys.argv)
machine – functions related to
processor itself
esp – functions related to the board
24. 24
1. Download the Thonny Python IDE at: https://thonny.org/
1. Download the MicroPython firmware from: MicroPython.org
1. Download the ESP32 USB driver at: CP210x USB to UART Bridge VCP Drivers
1. Install Thonny Python
1. Install the ESP32 USB driver
1. Flash a new firmware to ESP32 using the Thonny Python.
Refer this video : https://youtu.be/elBtWZ_fOZU
3.3 Installation
25. 25
Link to data sheet :
https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf
3.4 Pin Layout
27. 7/8/2020 Signal Processing and Radio Communications Lab confidential 27
Set pin mode
➔ Set a pin to act as an INPUT or OUTPUT pin.
Ex: p0 = Pin(0, Pin.OUT)
p0 = Pin(0, Pin.IN)
Pin Direction
➔ Assign a value to pins set as OUTPUT pins. (3.3v / 0 v)
Ex: p0.on() / p0.value(1)
p0.off() / p0.value(0)
Let’s begin with GPIO pins
29. 29
Reading an input pins
1. Reading Digital values
P2= Pin (0, Pin.IN) #create input pin on GPIO2
print(P2.value()) #get value, 0 or 1
1. Reading Analog values
● ADC functionality available on pins 32-39.
from machine import ADC
adc = ADC(Pin(32)) # create ADC object on ADC pin
adc.read() # read value, 0-4095 across voltage range 0.0v - 1.0v
adc.atten(ADC.ATTN_11DB) # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
adc.width(ADC.WIDTH_9BIT) # set 9 bit return values (returned range 0-511)
adc.read() # read value using the newly configured attenuation and width
30. 30
ADC.atten(attenuation)
This method allows for the setting of the amount of
attenuation on the input of the ADC. The possible
attenuation options are:
● ADC.ATTN_0DB: 0dB attenuation, gives
a maximum input voltage of 1.00v - this
is the default configuration
● ADC.ATTN_2_5DB: 2.5dB attenuation,
gives a maximum input voltage of
approximately 1.34v
● ADC.ATTN_6DB: 6dB attenuation, gives
a maximum input voltage of
approximately 2.00v
● ADC.ATTN_11DB: 11dB attenuation,
gives a maximum input voltage of
approximately 3.6v
ADC.width(width)
This method allows for the setting of the number of
bits to be utilised and returned during ADC reads.
Possible width options are:
● ADC.WIDTH_9BIT: 9 bit data
● ADC.WIDTH_10BIT: 10 bit data
● ADC.WIDTH_11BIT: 11 bit data
● ADC.WIDTH_12BIT: 12 bit data - this is
the default configuration
32. 32
● PWM can be enabled on all output-enabled pins.
● Currently the duty cycle has to be in the range of 0-1023.
from machine import Pin, PWM
pwm0 = PWM(Pin(0)) # create PWM object from a pin
pwm0.freq() # get current frequency
pwm0.freq(1000) # set frequency
pwm0.duty() # get current duty cycle
pwm0.duty(200) # set duty cycle
pwm0.deinit() # turn off PWM on the pin
pwm2 = PWM(Pin(2), freq=20000, duty=512) # create and configure in one go
3.5 Pulse Width Modulation (PWM)