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.
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
print("Hello World!")
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:
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.
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.
In the lectures, we used the "<" and the ">" conditions to find out, if the value in the variable a is bigger, smaller or equal to the value in variable b. Can you modify the program using "<" and "!=" to find out?
What happens if you test:
if a: print("yes") else: print("No")
Try this with a being assigned to zero and a being assigned to 7. What do you conclude?
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?
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
from math import sin,pi
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)).
Create a sin_deg 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.
If the above is too simple for you, you may try to calculate the throwing parabola. Let's say, a stone is thrown at a speed of 30m/s and an angle of 30° with respect to ground. Calculate the trajectory of the stone until it hits the ground. You way define a function trajectory, taking initial speed, the angle and the time resolution for which you want to calculate the stone positions.
v_hor = v_initial * cos(angle)
v_ver = v_initial * sin(angle)
x(t) = v_hor * t
y(t) = v_ver * t - 1/2 * g * t**2
Like this, you can easily calculate the trajectories for different angles and different initial speeds.
tp_10 is the trajectory for 10 degrees.
tp_45 is the trajectories for 45 degrees.
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:
def blink_once(delay):
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.
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.
The LED ring consists of 7 WS2812B addressable and cascadable LEDs. Each rgb LED consists of 3 tiny single color LEDs emitting red, green and blue light. The color you finally see is the mixture between these color components. The MicroPython driver for this type of LED is called NeoPixel. The color is defined as a tuple with three 8 bit color component intensities. 8 bits means that you can have values in the range 0..255. Since the LEDs are extremely bright, please restrict the values to 0..31.
Write a program trying to find out to which LED an LED number corresponds to. Where on the LED ring is LED0, LED1 ... Do this by lighting the LEDs one by one with only the red color component switched on. The green and blue are set to zero.
Then change the color. Try all color combinations (31,0,0), (0,31,0), (0,0,31) but also (31,31,0), (31,0,31) ... Which colors do you get?
You can further modify the colors by changing the intensity values. For example: (12,27,5) Play around trying to generate as many different colors as you like.
Write a program that lights the top LED in blue and then lights the following LEDs in clockwise direction to :
As a small project, we are going to combine the reading of the ADC and the control of the rgb LED ring. As we saw, the ADC delivers values between 0 and 4095 (12 bits). We can divide this range in 7 slices, corresponding to the 7 LEDs in the ring. For the lowest slice, the top LED will be lit. Then we go clockwise around the ring: the next LED lit will be to the right of the first one. For the highest slice, the middle LED will be lit.
Very often, the color indicates the signal level: For low signal levels, we use "cold" colors (blue, cyan) for higher levels we use "hot" colors (magenta, red, white). The color for the top LED will therefore be blue, the color for the middle LED will be white.
Here is the color sequence:
LED number | color |
---|---|
1 | blue |
2 | cyan |
3 | green |
4 | yellow |
5 | magenta |
6 | red |
0 | white |
Here is the exercise sheet in odt format:
https://iotworkshop.africa/pub/AFNOG/ExerciseSheets/exercises.odt
I | Attachment | History | Action | Size | Date | Who | Comment |
---|---|---|---|---|---|---|---|
![]() |
exercises.odt | r1 | manage | 144.3 K | 2022-10-20 - 12:45 | UliRaich | |
![]() |
gnuplot_sin.png | r1 | manage | 29.9 K | 2022-10-15 - 20:47 | UliRaich | |
![]() |
helloWorld.png | r1 | manage | 71.5 K | 2022-10-15 - 13:55 | UliRaich | |
![]() |
trajectories.png | r1 | manage | 57.5 K | 2022-10-22 - 13:36 | UliRaich |