Difference between pages "3 Bottom IO port control" and "5 Light up all kinds of LED"

From Banana Pi Wiki
(Difference between pages)
Jump to: navigation, search
 
 
Line 1: Line 1:
 
+
[[File:Webduino_gif.gif|thumb|Overview: [[BPI-Bit]]]]
 
[[File:Micropython3.png|thumb|[[1 Get the development suite]] ]]
 
[[File:Micropython3.png|thumb|[[1 Get the development suite]] ]]
 
[[File:Micropython2.png|thumb|[[2 Wired connection board]] ]]
 
[[File:Micropython2.png|thumb|[[2 Wired connection board]] ]]
Line 11: Line 11:
 
[[File:Micropython2.png|thumb|[[3 Mpfshell details]] ]]
 
[[File:Micropython2.png|thumb|[[3 Mpfshell details]] ]]
 
[[File:Micropython2.png|thumb|[[4 Pycharm IDE programming]] ]]
 
[[File:Micropython2.png|thumb|[[4 Pycharm IDE programming]] ]]
 +
  
 
[[File:Micropython5.png|thumb|[[1. Panel scrolling text]] ]]
 
[[File:Micropython5.png|thumb|[[1. Panel scrolling text]] ]]
[[File:Micropython2.png|thumb|[[2. Panel display images]] ]]
+
[[File:Micropython2.png|thumb|[[2. Panel display images]] ]]  
[[File:Micropython2.png|thumb|[[3 Bottom IO port control] ]]
+
[[File:Micropython2.png|thumb|[[3 Bottom IO port control]] ]]
 
[[File:Micropython2.png|thumb|[[4 Panel key detection]] ]]
 
[[File:Micropython2.png|thumb|[[4 Panel key detection]] ]]
 
[[File:Micropython2.png|thumb|[[5 Gets the board temperature]] ]]
 
[[File:Micropython2.png|thumb|[[5 Gets the board temperature]] ]]
Line 26: Line 27:
 
[[File:Micropython2.png|thumb|[[13 Codelab Scratch3]] ]]
 
[[File:Micropython2.png|thumb|[[13 Codelab Scratch3]] ]]
  
How to control the board's input and output!
+
[[File:Micropython6.png|thumb|[[1 The basic algorithm]]  ]]
 +
[[File:Micropython2.png|thumb|[[2 WiFI wireless connection]] ]]
 +
[[File:Micropython2.png|thumb|[[3 WiFI wireless programming]] ]]
 +
[[File:Micropython2.png|thumb|[[MQTT communication applications]]]]
  
IO in the computer refers to Input/Output, namely Input and Output, referred to as IO port.
 
  
[[File:Bitio.png]]
+
=Light up all kinds of LED=
  
*IO ports are different, some are larger, and in general, there are tags near them for users to understand. For example, the bottom of this board is distributed on goldfinger in the order of 0/1/2/3V/GND (most computers start from 0).
+
If Hello World is the beginning of religious programming for software programmers, Blink Led is the same for hardware programmers. All hardware programming starts with lighting a lamp.
  
*If you attach the gold finger to the base, other smaller blue and white line pins can be used as well, which was specifically noted in the introduction of the hardware in chapter 1. You can forget to check the [http://wiki.banana-pi.org/BPI-Bit#Hardware  BPI:bit hardware].
+
So we started to prepare our hardware, and I was using a beta version 1.3. It is sufficient for the purposes of this chapter, and is common to subsequent boards. Therefore, we pay attention to progressive, first a lamp, and then a row of lights.
  
*In this module of microbit, the interface of the board is defined as pinN object, where N represents the value of the interface, so the no. 0 interface is also called pin0 object. If you find this is not easy to use later, you can redefine your favorite Pin name by import machine Pin.
+
[[File:Ready.png]]
  
=Shy board=
+
==Use repl==
  
We want to touch the pin of the board and make it respond accordingly, and it looks like a shy little girl.
+
*Go into repl mode
  
code as below:
+
[[File:Into_repl.png]]
  
from microbit import *
+
*input (after selecting the text to copy, right-click in the black box and paste)
while True:
 
    if pin1.is_touched():
 
        display.show(Image.HAPPY)
 
    else:
 
        display.show(Image.SAD)
 
  
At this time, you need a hand to touch the pin of tag 1, you can see the board from sad to happy, if you release it, it will change the expression, is angry?
+
from machine import Pin
  
The procedure is explained as follows:
+
*input again
  
::On "pin1. Is_touched ()" repeatedly run to determine whether or not the pin is touched. (principle is ADC sampling)
+
Pin(18, Pin.OUT).value(1)
::It runs and returns True, which shows a smiley face, otherwise it always shows a bitter face, so it will feel sad when you don't touch it. (and so on! What kind of shyness is this!!!! )
 
  
The running effect is as follows:
+
[[File:Light_up.png]]
  
[[File:Touched_io.gif]]
+
*Make sure that one of the lights on the panel is lit
  
It should be noted that since we have static electricity in our body (hands), it may be electrocuted and cause its expression to twitch sometimes.
+
[[File:Light_result.png]]
  
=lamp ON or OFF=
+
*Just to make sure it's the light we're controlling,input
When we first started, we had already learned how to control the changes of LED lights on the board, but it was all done by others. So today we will have a try. How did others do it?
 
  
For this purpose, I have prepared the following common Led lights, and you will find that they are of different lengths. This is to distinguish their positive and negative poles. The positive pole is the long end, so the negative pole is the short end.
+
Pin(18, Pin.OUT).value(0)
  
Tip: the purpose of distinguishing the positive and negative poles is to hope that when you are wiring, the element (LED) positive pole is connected to the power positive pole (+), and the element (LED) negative pole is connected to the power negative pole
+
[[File:Light_down.png]]
  
[[File:Leds.jpg]]
+
*That's when you see it go out
  
Let's start with the first step, which is to turn on the ordinary light, that is, directly connect it (LED) to the power supply, then directly connect the big guy to the board of 3V and GND, 3V refers to the positive pole of the power supply (3V), GND refers to the negative level of the power supply, also known as the ground.
+
[[File:Light_restore.png]]
  
[[File:Leds1.gif]]
+
==Use mian.py==
 +
*Prepare the following code into the main.py file. Unlike the previous one, this effect is continuous. It will make the light on and wait for one second before it goes off.
 +
 +
from machine import Pin
 +
import time
 +
led = Pin(18, Pin.OUT) # get a led on gpio 18.
 +
print('turn on')
 +
led.value(1) # turn on
 +
print('sleep 1s')
 +
time.sleep(1) # sleep 1s
 +
print('turn off')
 +
led.value(0) # turn off
  
This lights up a light (pay attention to the positive and negative), but the light is not controllable, it will only light up, not out.
+
[[File:Mian_light.png]]
  
I wanted to take another lamp to connect it for comparison, but I found a problem that the other lights were too short, so I changed the following. I used our headlight to control, and it can be seen that the lamp will not be on at this time.
+
*If you feel the effect is not obvious, you can write an infinite loop to see the effect. Please use Ctrl + C to stop, otherwise you cannot continue the operation.
  
[[File:Led_off.jpg]]
+
from machine import Pin
 +
import time
 +
led = Pin(18, Pin.OUT) # get a led on gpio 18.
 +
while True:
 +
    print('turn on')
 +
    led.value(1) # turn on
 +
    print('sleep 1s')
 +
    time.sleep(1) # sleep 1s
 +
    print('turn off')
 +
    led.value(0) # turn off
 +
    print('sleep 1s')
 +
    time.sleep(1) # sleep 1s
  
So let's control its on and off by the following code, you can see in the picture I used pin2 pin to control.
+
[[File:Blink_led.png]]
  
from microbit import *
+
==Learn the LED light array (NeoPixel) on the light panel==
pin2.write_digital(1)
 
  
Now I use the Pycharm + Mpfshell plug-in to run this code.
+
*Ready the following code into main.py (\HowToCode\ 01.leds \rgb_lattice. Py)
  
[[File:Code_(1).png]]
+
from pixel import Pixel
 +
View = Pixel()
 +
RGB = (10, 10, 10)
 +
View.LoadXY(2, 2, RGB)
 +
View.Show()
  
You can see it lights up, so that means we can control the light, is that consistent with the initial tutorial?
+
*Execute using runfile main.py.
 
 
[[File:Led_on.jpg]]
 
 
 
So let's add a Blink effect this time, using the following code.
 
 
 
from microbit import *
 
 
 
while True:
 
    pin2.write_digital(1)
 
    sleep(200)
 
    pin2.write_digital(0)
 
    sleep(1000)
 
  
[[File:Blink.gif]]
+
[[File:Pixel.png]]
  
Let me explain the procedure:
+
At this point, the quick start of the tutorial has ended, from the lower toolbar back to the home page or read the material on the right, learn more basic, advanced, application and so on.
::Use pin2 pin to output 1, which will make the LED become high level. Simply think that this pin has voltage, the effect is equivalent to directly connected to the positive pole of the power supply.(the potential difference between the two pins should be understood in principle)
 
::Light it up first, namely pin2.write_digital(1), and then use sleep(200) to rest the board for 200 milliseconds.
 
::Then you put it out, which is pin2.write_digital(0), and rest for another 1,000 milliseconds, or one second.
 
::Do it all over again.
 

Latest revision as of 02:37, 21 February 2019

Overview: BPI-Bit




Light up all kinds of LED

If Hello World is the beginning of religious programming for software programmers, Blink Led is the same for hardware programmers. All hardware programming starts with lighting a lamp.

So we started to prepare our hardware, and I was using a beta version 1.3. It is sufficient for the purposes of this chapter, and is common to subsequent boards. Therefore, we pay attention to progressive, first a lamp, and then a row of lights.

Ready.png

Use repl

  • Go into repl mode

Into repl.png

  • input (after selecting the text to copy, right-click in the black box and paste)
from machine import Pin
  • input again
Pin(18, Pin.OUT).value(1)

Light up.png

  • Make sure that one of the lights on the panel is lit

Light result.png

  • Just to make sure it's the light we're controlling,input
Pin(18, Pin.OUT).value(0)

Light down.png

  • That's when you see it go out

Light restore.png

Use mian.py

  • Prepare the following code into the main.py file. Unlike the previous one, this effect is continuous. It will make the light on and wait for one second before it goes off.
from machine import Pin
import time
led = Pin(18, Pin.OUT) # get a led on gpio 18.
print('turn on')
led.value(1) # turn on
print('sleep 1s')
time.sleep(1) # sleep 1s
print('turn off')
led.value(0) # turn off

Mian light.png

  • If you feel the effect is not obvious, you can write an infinite loop to see the effect. Please use Ctrl + C to stop, otherwise you cannot continue the operation.
from machine import Pin
import time
led = Pin(18, Pin.OUT) # get a led on gpio 18.
while True:
   print('turn on')
   led.value(1) # turn on
   print('sleep 1s')
   time.sleep(1) # sleep 1s
   print('turn off')
   led.value(0) # turn off
   print('sleep 1s')
   time.sleep(1) # sleep 1s

Blink led.png

Learn the LED light array (NeoPixel) on the light panel

  • Ready the following code into main.py (\HowToCode\ 01.leds \rgb_lattice. Py)
from pixel import Pixel
View = Pixel()
RGB = (10, 10, 10)
View.LoadXY(2, 2, RGB)
View.Show()
  • Execute using runfile main.py.

Pixel.png

At this point, the quick start of the tutorial has ended, from the lower toolbar back to the home page or read the material on the right, learn more basic, advanced, application and so on.