| 1 | |
| 2 | |
| 3 | `Tutorial for learning to use KuttyPyNano - A Python to real world bridge` |
| 4 | |
| 5 | !!! info "Intended audience" |
| 6 | * Beginners who would like to explore microcontroller functions via Python without the compile+upload hassle. |
| 7 | * Hobbyists who want a way to control external parameters such as lights, fans, and [robots](../programming/intro/#slightly-complex-example) from their Python programs |
| 8 | * Scientists who want to use affordable and [easily available sensors](../sensors) for physical parameters( temperature, humidity, pressure...) without having to learn embedded systems and communications. |
| 9 | |
| 10 | |
| 11 | !!! info "" |
| 12 | {: style="align:center;max-width:400px"} |
| 13 | |
| 14 | ## Software Installation |
| 15 | |
| 16 | Download the kuttypy app from the google playstore. |
| 17 | |
| 18 | |
| 19 | ## Hello World |
| 20 | |
| 21 | This workshop will introduce you to the following basics of how to use a microcontroller to automate everyday |
| 22 | tasks. They will use the android app to change values of bits (0s and 1s) in various registers |
| 23 | via a friendly graphical interface, and enable you to observe the corresponding behaviour of the hardware |
| 24 | in real-time. No Compiling/Uploading of code is planned, and all input/output tasks are via the phone. |
| 25 | |
| 26 | ### Explore the playground |
| 27 | |
| 28 | Once you connect a KuttyPy nano to your phone via USB OTG cable, the titlebar should turn green |
| 29 | in colour and show the KuttyPy nano's picture below it. If not, close the app and try again. |
| 30 | |
| 31 | Click the playground. |
| 32 | |
| 33 | Here you will find a picture showing the pin numbers, and also the associated register+bit notation of that |
| 34 | pin. |
| 35 | |
| 36 | For example, D13 is labelled as PB3. This means that the output pin of the microcontroller which is |
| 37 | connected to D13 is controlled by the 3rd bit of the B register. |
| 38 | |
| 39 | !!! info "" |
| 40 | PB5 is also connected to the Fourth LED (Red) of the board, so turning it on will light up this LED. |
| 41 | |
| 42 |  |
| 43 | |
| 44 |  |
| 45 | |
| 46 | ### Connect MORE LEDS |
| 47 | |
| 48 | Similarly, you can also connect LEDs to other pins such as D2 (PD2), D3, D4... D12. |
| 49 | |
| 50 | Let us try to use D8 (PB0) . Connect a 1Kohm (1000 ohms) resistor from D8 pin to the longer leg(+) of an |
| 51 | LED. You can use a breadboard to make these connections as shown. Then the shorter leg of the LED(-) should |
| 52 | be connected to GND to complete the circuit. |
| 53 | |
| 54 |  |
| 55 | |
| 56 | Now tap on PB0 in the playground, and change the output level from L to H to light up this LED. |
| 57 | |
| 58 | !!! tip "" |
| 59 | When the pin is shown as IN type in the playground, the L/H icon indicates the state of the pin |
| 60 | in input mode. If voltage on the pin is near 0 Volts, it is L, and if it is above 3 Volts, it is H. |
| 61 | |
| 62 | |
| 63 | ## What are registers, and how to blink an LED |
| 64 | |
| 65 | We shall now learn to write programs. |
| 66 | |
| 67 | ### Getting Started |
| 68 | |
| 69 | + `Go to Visual Programming -> Getting Started -> Hello World` |
| 70 | |
| 71 | The program shown has 2 parts. |
| 72 | |
| 73 | + A print block : This prints(displays on the screen) the value of anything connected to it. |
| 74 | + A label whose value is set to `hello` |
| 75 | |
| 76 | You will find a blue button called 'RUN' at the top left corner. This is used to run your simple program. |
| 77 | Click it . |
| 78 | |
| 79 | Click it again to close the results window, and edit your program. |
| 80 | |
| 81 | There are also buttons to temporarily save and restore your work, and also a `help` button for instructions. |
| 82 | |
| 83 | On the left are various components you can use to design your program |
| 84 | |
| 85 | ### Blink an LED |
| 86 | The LED you connected to PB0 (D8) will again be used . |
| 87 | + `Go to Visual Programming -> KuttyNano -> Blink LED` |
| 88 | |
| 89 |  |
| 90 | |
| 91 | Observe this program carefully |
| 92 | |
| 93 | + the 0th bit(Right-Most) of DDRB(Data Direction Register for B) is selected (Set to 1). |
| 94 | + This means that PB0 is is now configured as an output. you can connect LEDs and other small loads to it. |
| 95 | + A `while` loop with the condition set to `true` is added below it |
| 96 | + Anything inside a while loop will run in a loop continuously as long as the condition is `true`. |
| 97 | + The 0th bit of PORTB( Used to set the voltage level of output pins) register is selected. |
| 98 | + Now the LED should be on, so we wait for 2 seconds so you can see it is ON. |
| 99 | + Then the 0th bit is made 0, so PB0 will be set to 0Volts. |
| 100 | + So the LED will be off, and we now wait another 2 seconds. |
| 101 | + The cycle of On OFF will keep repeating forever until you abort the program. |
| 102 | |
| 103 | ### Traffic Lights |
| 104 | |
| 105 |  |
| 106 | |
| 107 | We will use 3 LEDs in this demo, and light them up one by one to show a traffic light signal. |
| 108 | |
| 109 | ### AI control using gestures |
| 110 | |
| 111 | + `Go to Visual Programming -> Visual Demos -> AI LED` |
| 112 | |
| 113 | Downloading for AI assets is around 10MB, so wait for the download to complete after you |
| 114 | grant permissions to use the selfie camera. |
| 115 | |
| 116 | Once you are able to see yourself in the screen, raise your hand, and observe the algorithm |
| 117 | detecting the various joints in your fingers. Each joint is labelled as shown below |
| 118 | |
| 119 |  |
| 120 | |
| 121 | + This program lights up the Onboard LED (PB5) if the index finger is extended |
| 122 | + To check this, we use the `is Index Finger Extended?` logic block which returns `true` if it is, and `false` if not |
| 123 | + It checks where the Y coordinate of joint 8 is more than the Y coordinate of joint 6. |
| 124 | + So when you fold your finger, 8 comes below 6, and the LED is turned off since the condition is `false` |
| 125 | |
| 126 | ### AI Piano |
| 127 | |
| 128 | Flex fingers one by one to play different notes on a virtual piano. |
| 129 | |
| 130 | ### AI Hand gestures |
| 131 | |
| 132 | We will plot a graph of the distance between the tip of the thumb to the tip of the index finger . |
| 133 | |
| 134 |  |
| 135 | |
| 136 | ## Sensors using I2C communication |
| 137 | |
| 138 | * [I2C Sensors](../sensors) for a range of physical parameters such as pressure, acceleration etc can be connected using SCL and SDA pins. |
| 139 | * Use the Data Logger interface to scan for sensors and view readings |
| 140 | * Explore potential applications. |
| 141 | |
| 142 | ### Pressure measurement with BMP180 |
| 143 | |
| 144 | In the soldering workshop, you will have connected 4 pins to a BMP180 pressure sensor. We will now learn to use it. |
| 145 | |
| 146 | + This sensor uses 2 pins to exchange data with your kuttypynano |
| 147 | + SDA (PC4 / A4) : For data |
| 148 | + SCL (PC5 / A5) : Clock for synchronizing data |
| 149 | + You also need to connect 5V and GND for power supply to your sensor. |
| 150 | + Total Connections are as follows |
| 151 | + 5V -> VCC |
| 152 | + GND -> GND |
| 153 | + SCL -> SCL |
| 154 | + SDA -> SDA |
| 155 | |
| 156 |  |
| 157 | |
| 158 | ## Basic C code |
| 159 | |
| 160 | This is not covered in this tutorial, but a precompiled code for blinking LEDs on pins PD0-PD6 is |
| 161 | part of the app. To upload this code to your device, go to the home page, and click the menu at the top |
| 162 | right corner |
| 163 | |
| 164 | + Upload HEX ->LED Display 328p |
| 165 | + Click on the menu again, and click on disconnect. |
| 166 | + Your program is now running. :) |