I am new to coding for Arduino. I have done Java and Matlab in the past.
I have designed a simple replay circuit as seen here:
I assume it is set up right, but if not please let me know.
I basically need simple code to just trigger the relay on and off for intervals of 1 second indefinitely or until I just disconnect the power.
Any help would be great.
Thanks!
You can simply use the Blink example that comes with Arduino IDE. Connect the Relay output control to pin 13 (your yellow wire) and you have it.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Even better would be to use the blink without delay example
Related
Here is my code:
#define LED_BUILTIN 12
// the setup function runs once when you press reset or power the board
void setup()
{
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop()
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW);// turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
The above code works perfectly fine for GPIO_2 but fails for GPIO_12. I want to ask whether it is giving fault because I am using touchpad pin or there is some other error?
I have attached an LED to GPIO_12 of ESP32 and after program uploading, my led is not blinking. I have tried to connect voltmeter to GPIO_12 and it is giving 0.5 volts with minor fluctuation. I was expecting a change of voltage after each second as per HIGH/LOW conditions of my code but unfortunately, I am not getting the desired result. What could be the possible reason?
Now I have found out the solution so let's share so that others can also get benefit. The code for which I was following the reference website contained 36 pins which mean 18 pins on each side but in my version of the board there were total 30 pins on my board which mean 15 pins on each side. Now that I have got pin out reference of that board and my board also, everything is fine, aligned and makes perfect sense. Problem was that the hardware which the reference website referred to had different hardware version with 36 pins and my hardware contained 30 total pins and was a different version of ESP32.
I'm using Micro python to try and generate an RGB signal for a TFT screen such as the:
Screen
The timings i need the follow are:
CLKIN: 3.33Mhz
Horizontal is 800 clock cycles up and 1 clock cycle down
Vertical is 480 Horizontal Clock cycles up and
Ive already got this code that spits out the clock but i cant chnage the square function or add a second state machine, how would i go about bit bashing the pins at the required times.
from machine import Pin
from rp2 import PIO, StateMachine, asm_pio
import time
#asm_pio(set_init=PIO.OUT_LOW)
def square():
wrap_target()
set(pins, 1)
set(pins, 0)
wrap()
sm = rp2.StateMachine(0, square, freq=33300000, set_base=Pin(2))
sm.active(1)
Your frequency is 3.33mHz, but each cycle uses 2 cpu cycles so each rising edge is only at 1.665mHz.
I'm trying to read the state of the input pin (BOARD pin 6, which is a ground pin) and I receive the error "ValueError: The channel sent is invalid on a Raspberry Pi".
Am I misunderstanding the definition of an input pin? My understanding was that it is simply the ground/negative pin, connecting back 'in' to the pi?
I'm trying to read the state purely for tinkering purposes, to see the value change when it's floating (not using a pull-down).
The Ground pin is connected, literally, to ground. It is impossible to read or write values to ground or power, as these are the circuit components. You have to connect to a GPIO pin (the green(ish? I'm colorblind) dots at http://pinout.xyz).
It is possible for the input of a GPIO pin to be set to HIGH or LOW, depending on the circuit you wish to use. If you expect the GPIO to be normally LOW and HIGH when your input is triggered (such as with a pushbutton switch), then you have to set the state to pulldown.
I would recommend you read some of the background on microcontrollers: https://embeddedartistry.com/blog/2018/06/04/demystifying-microcontroller-gpio-settings/
I want to configure SPI1 in stm32f103. Why is it required to make pins AF(Alternate function)? Cant it just be input or output? What is actually happening when the pins are AF?
Those pins can be connected to several different peripheral blocks, by setting the alternate function, you're configuring a multiplexer within the IC to connect the pin to the correct peripheral block for your purpose.
Output pins can be controlled either by the GPIO ODR registe, or a peripheral. There must be a way to say which one has the control. If you don't need all output signals of a peripheral, you can leave the corresponding pin as GPIO, and use the pin for something else.
If the pin is set as General Purpose Output, then the output voltage level is determined by the ODR register. The peripheral controller has no effect on the output.
If the pin is set as Alternate Function Output, then the peripheral determines the pin output, and the corresponding ODR register is disconnected from the pin.
If the pin is set as input, then there is no difference. The pin state is available both in the IDR register and to the peripheral controller.
In your STM32F103, Page 26 describes what would be the value of each pin after reset. Making it AF (alternate Function) tells the processor to make this pin as your desired functionality, SPI1 in your case. If you dont do it then your Pin will always behave as default Pin.
I know if I do too many thing in update() the consequence would be dropped frame rate below the target frame rate. But what would happen if I do the same thing in fixedUpdate()?
Would it cause Unity's physicsEngine to mess up, or would it crash the program?
The answers and comments were very helpful, but they lack a clear, informative answer to my question. Everyone knows bad things will happen when you put too much work load in the FixedUpdate() loop, my question was to ask what bad thing will happen then.
Since I finally got my hand on a computer I decided to make some test on my own. Just for reference this is what I used to test:
public class TestFixedUpdate : MonoBehaviour {
public int loopNo = 500000;
private int noOfCall = 0;
private int collisionTimes = 0;
private void FixedUpdate()
{
if (noOfCall > 100) return;
float time = Time.timeSinceLevelLoad;
for (int i = 0; i < loopNo; i++) {
Quaternion.Slerp(Quaternion.identity, Quaternion.FromToRotation(Vector3.up, Vector3.forward), Mathf.Abs(Mathf.Sin(Time.timeSinceLevelLoad)));
}
Debug.Log(Time.timeSinceLevelLoad.ToString("0.00"));
if (noOfCall > 99) Debug.Log("Simulation finished. Times collided:" + collisionTimes);
noOfCall++;
}
private void OnCollisionEnter(Collision collision)
{
if (noOfCall > 100) return;
collisionTimes++;
Debug.Log("Times collided:" + collisionTimes);
}
}
I put it on a sphere that will continuously bounce on a plane. The test was made by changing the function from FixedUpdate() and Update() and compare the differences.
The main difference I found out was that in the case of FixedUpdate(), Unity's simulated Time (the game world's time) is delayed out of sync with actual time. In other words, any function that depends on Unity's Time system will act as if the whole world has slowed down.
Also:
Unity reports the same frame rate for both cases, even though for the FixedUpdate() case it is apparent that the actual frame rate - the frame per real world second - is significantly lower.
Aside from slowing down, collision detection and physics simulation logic seem to work as usual. Bodies that should collide still collide; acceleration, force application etc still work. Rigidbodies that don't jump through a collider in 20ms (Unity's ms) still collide and bounce back as usual.
The Maximum Allowed Timestep option in Project's settings define the maximum amount of (Unity's simulated time) before a frame must be draw (I never knew what it was for before). Clarification: if I set it to 0.5 second, then no matter how much code I put into both functions, the logic update step (Update()) will be called immediately after a FixedUpdate() round has simulated the 0.5-th second, and then the scene will be rendered.
The engine will stall while processing a heavy load (e.g. if you do an infinite loop) inside any function. Things which rely on Time.DeltaTime will start acting up if the frame rate drops too low. It won't really matter whether that's Update or FixedUpdate.
What you can do if you need a process to run a heavy load is to use an coroutines e.g. IEnumerator function and yield, which allows you to split processing over a number of frames, or to have some function called via a callback. e.g. if you have an AI check path 5 times a second you can either handle that in Update with a counter each frame or you can schedule a callback.
Basically, there's going to be limit in how much code you can run per frame without degrading performance. Clever rewriting should make that unnecessary. See if you can cache results of calculations or pre-calculate as much data outside play as possible.