Tags:
view all tags
---+ Exercises #FirstSession ---++ First exercise session: ---+++ A bit of programming Connect your ESP32 to the PC using the micro USB cable and start thonny. Locate the shell window and check if you see the PEPL prompt ">>>". If not, try to press <enter>. Make sure the shell window has the input focus. If this does not work, press the stop button. If this also doesn't help, press the reset button on the ESP32 CPU board. ---+++ Playing with REPL * print "Hello World!" * calculate 127,9 * 157.6 * 17.2 /(3.5*2**4) 2**4 means 2 to the power of 4<br />you may try to calculate e.g 2**4 separately first and check that you get the result expected * continue experimenting with REPL ---+++ Hello World Instead of working on the shell window, we will now write a Python script (a small program). For this to work, select File -> New. This will create an editor window named <untitled>. Save this empty program to the disk on your PC. Select File -> Save, select "This Computer" give it a file name (e.g. hello.py) and press "OK". The window title changes to hello.py. Now enter <verbatim>print("Hello World!")</verbatim> Finally, run the program by pressing the green button with the white triangle. You should see "Hello World!" printed in the shell window. This is what you should get: <img alt="helloWorld.png" height="685" src="%ATTACHURL%/helloWorld.png" title="helloWorld.png" width="1046" /> ---+++ Some Arithmetic Assign 2 values to 2 variables a and b. Calculate the sum, the difference, the product and the division and print the results. Start assigning integer values. Try integer and float division. Then assign float values and try again. ---+++ Conditions Assign again 2 values to 2 variables a and b with a being bigger than b . In your program, check if a is equal, bigger or smaller than b. Modify the values you assign to a and b, with b now bigger than a and try again. What happens if you test: <verbatim>if a: print("yes") else: print("No")</verbatim> Try this with a being assigned to zero and a being assigned to 7. What do you conclude? ---+++ Loops Write a script that prints "Hello World!" forever. You can stop the program with the red Stop button. Write a program that prints "Hello World!" 7 times. Do this using a while loop and in a second program, using a for loop, Which of the programs is more elegant? ---++++ This is the end of the first exercise session #SecondSession ---++ Second exercise session ---+++ !MicroPython modules Modify the loop program in such a way that "Hello World!" is printed only every second. Import the utime module and make the program sleep for 1s after each print statement. Import the sin function and the value of pi from the math module <verbatim>from math import sin,pi</verbatim> Remember that the sin function takes its values in units of radians. The angle values for a complete sin period will range 0 .. 2*pi. Print the angles and the sin values for a full sin period using 30 equidistant angle values. (x and sin(x)). ---+++ Creating and using your own function Create a<i> sin_deg</i> function which takes angles in units of degrees instead of radians. Print the angles and the sin values for 36 equidistant angle values. You may copy/paste the result to a new editor window in thonny and save them to a file on the PC. Then you can plot the function using gnuplot. <img alt="gnuplot_sin.png" height="598" src="%ATTACHURL%/gnuplot_sin.png" title="gnuplot_sin.png" width="662" /> ---++++ End of the second exercise session #ThirdSession ---+++ Programming the user LED on the CPU card Write a program that blinks the user programmable LED on the CPU card at a frequency of 1 Hz (500 ms on, 500 ms off) Write a function that blinks the LED once with a delay that is passed as a parameter: <verbatim>def blink_once(delay): </verbatim> Test the function with a main program calling is with different delays. Use the function to implement a program that blinks an SOS: 3 short pulses, followed by 3 long pulses, followed by 3 short pulses, followed by a 2s pause. You may use a 200 ms delay for the short pulses and 700 ms delay for the long ones. ---+++ Reading the pushbutton Write a program that reads the push button state ever 100 ms and prints its state (pressed or released) Improve the program printing the state only when there was a state change. # Session 4 ---+++ Reading an analogue signal level from the slider potentiometer -- %USERSIG{UliRaich - 2022-10-14}% ---++ Comments %COMMENT%
Attachments
Attachments
Topic attachments
I
Attachment
History
Action
Size
Date
Who
Comment
png
gnuplot_sin.png
r1
manage
29.9 K
2022-10-15 - 20:47
UliRaich
png
helloWorld.png
r1
manage
71.5 K
2022-10-15 - 13:55
UliRaich
Edit
|
Attach
|
Watch
|
P
rint version
|
H
istory
:
r14
|
r8
<
r7
<
r6
<
r5
|
B
acklinks
|
V
iew topic
|
Raw edit
|
More topic actions...
Topic revision: r6 - 2022-10-16
-
UliRaich
Home
Site map
AFNOG web
Embedded_Systems web
IoT_Course_English web
IoT_Course_French web
Main web
Sandbox web
TWiki web
AFNOG Web
Create New Topic
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
P
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
Account
Log In
Register User
Edit
Attach
Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback